Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Ultimately, 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!

like image 332
Sanjamal Avatar asked Dec 01 '09 17:12

Sanjamal


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

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.

like image 68
Pavel Minaev Avatar answered Oct 25 '22 19:10

Pavel Minaev


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.

like image 32
Reed Copsey Avatar answered Oct 25 '22 18:10

Reed Copsey