内容目录
Through reflection, after obtaining the property names and attributes, the next step is to retrieve the values of the properties.
static void Main(string[] args)
{
Test test = new Test()
{
A = 13510377651,
B = 1,
C = 13510399648
};
Type type = test.GetType();
// Get the list of properties in the class
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo item in properties)
{
MyAttribute att = (MyAttribute)Attribute.GetCustomAttribute(item, typeof(MyAttribute));
if (att != null)
{
Console.WriteLine(att.Name + "|" + item.Name + "|" + item.GetValue(test) + "|Is it a phone number: " + IsValid(item.PropertyType, item.GetValue(test)));
}
}
Console.ReadKey();
}
private static bool IsValid(Type type, object value)
{
if (type != typeof(long))
return false;
long phone;
bool num = long.TryParse(new ReadOnlySpan<char>(value.ToString().ToCharArray()), out phone);
if (num == false)
return false;
if (phone < 100_000_000_00)
return false;
return true;
}
}
public class Test
{
[My("1")]
public long A { get; set; }
[My("2")]
public long B { get; set; }
[My("3")]
public long C { get; set; }
}
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter,
AllowMultiple = false)]
public class MyAttribute : Attribute
{
public string Name { get; set; }
public MyAttribute(string name)
{
Name = name;
}
}
Output:
1|A|13510377651|Is it a phone number: True
2|B|1|Is it a phone number: False
3|C|13510399648|Is it a phone number: True
An example of using reflection on a type is as follows:
// Usage:
// Access or modify the instance field myField
MyClass myObj = new MyClass() { myField = 1 }; // Create an instance
Type myType = typeof(MyClass); // Get the type, or myObj.GetType()
FieldInfo fieldInfo = myType.GetField("myField"); // Get the specified field info from the type
Console.WriteLine((int)fieldInfo.GetValue(myObj)); // 1, get the value of the instance field
fieldInfo.SetValue(myObj, 2); // Assign a value to the instance field
// Access or modify the static field myStaticField
FieldInfo staticFieldInfo = myType.GetField("myStaticField"); // Get the specified field info from the type
Console.WriteLine(staticFieldInfo.GetValue(null)); // 0, get the value of the static field
staticFieldInfo.SetValue(null, 2); // Assign a value to the static field
public class MyClass
{
public int MyProperty { get; set; }
public static int MyStaticProperty { get; set; }
}
// Usage:
// Access or modify the instance property MyProperty
MyClass myObj = new MyClass() { MyProperty = 1 }; // Create an instance
Type myType = typeof(MyClass); // Get the type, or myObj.GetType()
PropertyInfo propertyInfo = myType.GetProperty("MyProperty"); // Get the specified property info from the type
Console.WriteLine((int)propertyInfo.GetValue(myObj, null)); // 1, get the value of the instance property
propertyInfo.SetValue(myObj, 2, null); // Assign a value to the instance property
// Access or modify the static property MyStaticProperty
PropertyInfo staticPropertyInfo = myType.GetProperty("MyStaticProperty"); // Get the specified property info from the type
Console.WriteLine(staticPropertyInfo.GetValue(null, null)); // 0, get the value of the static property
staticPropertyInfo.SetValue(null, 2); // Assign a value to the static property
※ When using reflection to assign values to properties, an ArgumentException will be thrown if the property does not have a set accessor.
文章评论