[英]In Reflection, how to set a correct value for a property?
My target is to find all the "string" type properties, and assign them to a specific string value, like "This is a testing string". 我的目标是找到所有的“字符串”类型属性,并将它们分配给特定的字符串值,例如“这是测试字符串”。
I can find all the string type properties in a class now, but always have problems when assigning string value to the property in a class, which is the class property of another class. 我现在可以在一个类中找到所有的字符串类型属性,但是在将字符串值分配给一个类中的属性(另一个类的类属性)时总是会遇到问题。
public class Credit_Card
{
public string brand { get; set; }
public int billing_phone { get; set; }
public string credit_card_verification_number { get; set; }
public Expiration expiration { get; set; }
}
public class Expiration
{
public string month { get; set; }
public string year { get; set; }
}
class Program
{
static void Main(string[] args)
{
Credit_Card credcard = new Credit_Card { brand = "Visa", billing_phone = 12345, credit_card_verification_number = "1234", expiration = new Expiration { month = "11", year = "2016" } };
foreach (PropertyInfo prop in GetStringProperties(credcard.GetType()))
{
prop.SetValue(credcard,"testing string!!",null);
Console.WriteLine(prop.GetValue(credcard,null));
}
Console.ReadLine();
}
public static IEnumerable<PropertyInfo> GetStringProperties(Type type)
{
return GetStringProperties(type, new HashSet<Type>());
}
public static IEnumerable<PropertyInfo> GetStringProperties(Type type, HashSet<Type> alreadySeen)
{
foreach (var prop in type.GetProperties())
{
var propType = prop.PropertyType;
if (propType == typeof(string))
yield return prop;
else if (alreadySeen.Add(propType))
foreach (var indirectProp in GetStringProperties(propType, alreadySeen))
yield return indirectProp;
}
}
}
It always throws the exception when the loop runs to "month" property of Expiration class. 当循环运行到Expiration类的“ month”属性时,它将始终引发异常。
How can I assign the correct value into the correct instance? 如何将正确的值分配给正确的实例?
If you wanted to assign month
and year
values you would need to get their PropInfo by doing Type.GetType(CredCard.Expiration).Properties
because they aren't properties of credcard
but are properties of credcard.Expiration
. 如果要分配month
和year
值,则需要通过执行Type.GetType(CredCard.Expiration).Properties
.Properties来获取其PropInfo,因为它们不是credcard
的属性, credcard.Expiration
属性。
You would have to check for the properties and assign them to credcard.Expiration
rather than credcard
. 您将必须检查属性并将它们分配给credcard.Expiration
而不是credcard
。
// prop.Name == "month" || prop.Name == "year"
prop.SetValue(credcard.Expiration, "somestring");
Another alternative would be to assign a new Expiration object to the Expiration property: 另一种选择是将新的Expiration对象分配给Expiration属性:
if(prop.Name.Equals("Expiration"))
{
var expiration = new Expiration
{
month = "someString",
year = "someString"
};
prop.SetValue(credcard,expiration);
}
Looking at the code, you are grabbing all string properties on Credit_Card
then recursively grabbing all string properties on child objects. 查看代码,您将获取Credit_Card
上的所有字符串属性,然后以递归方式获取子对象上的所有字符串属性。
Your problem is coming from the fact that it is returning string month
from Expiration
. 您的问题来自于它从Expiration
返回string month
的事实。 You then are attempting to set the Expiration.month
property on your Credit_Card
instance credcard
. 然后,您尝试在Credit_Card
实例credcard
上设置Expiration.month
属性。
That does not work. 那行不通。 The target must match the same Type
that declared the PropertyInfo
. 目标必须匹配声明PropertyInfo
的相同Type
。 This is why you are getting: 这就是为什么您得到:
System.Reflection.TargetException was unhandled, and the messagei is "Object does not match target type" 未处理System.Reflection.TargetException,消息为“对象与目标类型不匹配”
You need to split your loop up so you do them in correct hierarchy chain and have the correct instance (target) that will be updated with your data. 您需要拆分循环,以便按正确的层次结构链进行操作,并具有将随数据更新的正确实例(目标)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.