开发者

C# Type Casting at Run-Time Using Reflection

Judging by the title of this question, what I want to do may not be possible so I will describe what I'm doing and you can feel free to let me know what I'm doing wrong and what would be a better way to accomplish my goal.

I have an XML file that describes 1) a custom object that is derived of a base type, and 2) the internal field names and associated values. These derived objects may have additional fields that the base class doesn't know about.

I extract the type of object as a string and I place all the object data into a dictionary where the key is the field name and the value is the field's value. I instantiate the object using the string name and the object's default constructor. I can sniff out all the object's properties into a PropertyInfo[]. Now I need to take all the values that are in string form and convert them into the correct data types so I can load them into the object's properties. (As I type this, it sounds like I'm taking a sort of save state and restoring it, but I've only heard of this sort of thing. If you would like to advise along these lines, then please keep in mind that I'm stuck reading data from an XML file and I can't change that.)

I can convert all the string values to a type given by a Type object and using a brute-force function that I made having the following definition:

public object convertMe(string v, Type t)

Since I don't know what Type I'm throwing at the function, I don't know what's coming back and I am unable to explicitly cast the object to the proper type for assigning to the aforementioned property. I have been attempting the following sort of casts to no avail:

string objectType = /*read type string from XML*/;

... // Wherein I instantiate an object "theObject" and get PropertyInfo[] from it.
... // I also make sure that I'm trying to assign the correct data to
... // the correct property.

Type t = currentProperty.PropertyType;
object o = convertMe(value, Type.GetType(qtype));
currentProperty.SetValue(theObject, (t)o, null);  // Doesn't work
currentProperty.SetValue(theObject, (t)Convert.ChangeType(o, t), null);  // Doesn't work.  Apparently (t) is bad syntax.

Ultimate开发者_如何转开发ly, my goal is to create an instance of the object and load it with data from the XML file in such a way that the data types aren't hard-coded. The reason that I'm doing this in C# is because this used to be a Python program and I've been assigned to translate it into C# .NET 2.0. It's a learning experience to say the least. The reason that I created that "brute-force function" is because I was looking for a way around this casting problem, but no matter what I try I can't get it to work. I could likely do this on my own using pure force, but I figured that there must be an elegant solution that I'm missing.

Any help is greatly appreciated!


You don't need any cast there at all. PropertyInfo.SetValue takes an argument of type object, so just pass o to it, and that will be it.


You don't need the cast at all. Casting is only useful when you're trying to deal with the objects directly, not when you're using reflection.

When you go to set the property, you're using PropertyInfo.SetValue. This takes an object as the value parameter, so there is no need to try to coerce this into the specific type (as long as the object is already the correct type).

Since you've already done a "convertMe" that puts the object into the correct type (cast or boxed into an Object), you're all set - just use it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜