Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an instance of value types using reflection

I want to create an instance of value types like System.String, System.Boolean, System.Int32, etc. I get qualified names of types like System.String or MyNamespace.Employee and I have to create an instance and return back. I use Activator.CreateInstance and FormatterServices.GetUninitializedObject to create instances. But it fails in case of value types. I cannot hard code the logic in case of value types. I need a generic way of creating instances of both value types and reference types.

like image 858
Hemanshu Bhojak Avatar asked Jan 04 '10 06:01

Hemanshu Bhojak


People also ask

What is reflection explain its use with example?

Reflection provides objects (of type Type) that describe assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.

What is reflection where can it be used C#?

Reflection in C# is used to retrieve metadata on types at runtime. In other words, you can use reflection to inspect metadata of the types in your program dynamically -- you can retrieve information on the loaded assemblies and the types defined in them.

What is System reflection?

Reflection is the process of describing the metadata of types, methods and fields in a code. The namespace System.Reflection enables you to obtain data about the loaded assemblies, the elements within them like classes, methods and value types. Some of the commonly used classes of System.Reflection are: Class.

Where are namespace reflection API related classes located?

The classes in the System. Reflection namespace, together with System. Type, enable you to obtain information about loaded assemblies and the types defined within them, such as classes, interfaces, and value types (that is, structures and enumerations).


3 Answers

What exactly is it you are trying to do? FormatterServices.GetUninitializedObject is used mainly by serialization code; outside of that you shouldn't really use it. It sounds like you might just need something like TypeConverter, i.e. (for these types)

TypeConverter tc = TypeDescriptor.GetConverter(someType);
object obj = tc.ConvertFromString(s);
like image 89
Marc Gravell Avatar answered Oct 18 '22 23:10

Marc Gravell


What exactly is failing? I tried the following code to see if there is a problem with value types:

var instance = Activator.CreateInstance(typeof(Int32));

It gives me an Int32 instance set to 0.

Where exactly is your code failing? Otherwise I would say the problem lies with the way you are loading the type, not the way you are creating the instance.

like image 3
Jaco Pretorius Avatar answered Oct 18 '22 23:10

Jaco Pretorius


For BCL Value Types (and when using Strings to describe types) ensure you are not using C# keywords and ensure the Type is fully qualified with namespace. For example, C# int is successfully created this way with Activator.CreateInstance(..)

    object num = Activator.CreateInstance(Type.GetType("System.Int32"));

You will get failed attempts if you try to use language-specific aliases like "int" or short forms like "Int32".

like image 1
John K Avatar answered Oct 19 '22 00:10

John K