Dynamically creating objects using reflection
-
Hi All, I am trying to dynamically create object based on values that being submited from a form, but keep on getting a "No parameterless constructor defined for this object." error.
Type paramType = Type.GetType("System." + Request["type"]); //The Type of object String paramValue = Request["val"]; //the value if (paramType != null) { Activator.CreateInstance(paramType); }
-
Hi All, I am trying to dynamically create object based on values that being submited from a form, but keep on getting a "No parameterless constructor defined for this object." error.
Type paramType = Type.GetType("System." + Request["type"]); //The Type of object String paramValue = Request["val"]; //the value if (paramType != null) { Activator.CreateInstance(paramType); }
What are the types that you are trying to create, and do they all have public, parameterless constructors? -Phil
-
What are the types that you are trying to create, and do they all have public, parameterless constructors? -Phil
-
Hi Phil, Right now I am just trying to create strings and ints, but will have to create other types of objects. All will be public but some will be parameterless constructors some will not. Thanks, tom
The System.String type does not have a parameterless constructor (they all require at least one parameter), and therefore cannot be instantiated via the Activator.CreateInstance(Type) method. -Phil
-
The System.String type does not have a parameterless constructor (they all require at least one parameter), and therefore cannot be instantiated via the Activator.CreateInstance(Type) method. -Phil
thanks Phil, Would you mind showing me a quick example of how to create a string via reflection. This is what I tried using before but it allways returns null.
Type[] ArgTypes = new Type[1] {Type.GetType("System.String") }; ConstructorInfo oConstructorInfo = paramType.GetConstructor(ArgTypes );
thanks -
thanks Phil, Would you mind showing me a quick example of how to create a string via reflection. This is what I tried using before but it allways returns null.
Type[] ArgTypes = new Type[1] {Type.GetType("System.String") }; ConstructorInfo oConstructorInfo = paramType.GetConstructor(ArgTypes );
thanksHere's one way to create the string "a" via reflection:
Type type = Type.GetType("System.String"); object obj = Activator.CreateInstance(type, 'a', 1);
In this case, the .NET runtime would use the System.String.String(char, int) constructor in order to create the instance because we passed a character and an integer to Activator.CreateInstance(). In the case of the .NET native types like strings and integers, however, if you already know what the value of the object will be, using reflection is overkill. There are far easier ways to translate between one native type and another. -Phil -
thanks Phil, Would you mind showing me a quick example of how to create a string via reflection. This is what I tried using before but it allways returns null.
Type[] ArgTypes = new Type[1] {Type.GetType("System.String") }; ConstructorInfo oConstructorInfo = paramType.GetConstructor(ArgTypes );
thanksYou could Create a class called MyString add a parameterless constructor and an implicit cast public static implicit operator string (MyString A) { return A._internalStringVariable; } public static implicit operator MyString (String S) { return new MyString(S); } Then you'll have an object that behaves a bit like a string but has a parameterless constructor. HTH Russ