Activator.Create instance - please save my sanity
-
I'm using the following code to create an instance of a Type: Activator.CreateInstance(propertyType) // Where propertyType is a Type This seems to work fine for the majority of types but when I attempt to use it for a string I get the following error: No parameterless constructor defined for this object. I've been looking high and low for any examples or documentation on the how to do this and haven't been able to find anything. Can anybody help and save my sanity?
-
I'm using the following code to create an instance of a Type: Activator.CreateInstance(propertyType) // Where propertyType is a Type This seems to work fine for the majority of types but when I attempt to use it for a string I get the following error: No parameterless constructor defined for this object. I've been looking high and low for any examples or documentation on the how to do this and haven't been able to find anything. Can anybody help and save my sanity?
Lowest of the Low wrote:
No parameterless constructor defined for this object.
There is no parameterless constructor for String[^] class.
Activator.CreateInstance
method requires one. You can use thisCreateInstance(Type, object[])
overloaded method. Check this link http://msdn2.microsoft.com/en-us/library/system.activator.createinstance.aspx[^]*jaans
-
I'm using the following code to create an instance of a Type: Activator.CreateInstance(propertyType) // Where propertyType is a Type This seems to work fine for the majority of types but when I attempt to use it for a string I get the following error: No parameterless constructor defined for this object. I've been looking high and low for any examples or documentation on the how to do this and haven't been able to find anything. Can anybody help and save my sanity?
Activator.Create instantiates an object using its default constructor. As you can see here[^], string has no default constructor. Activator.Create is probably identical to the following:
public object CreateInstance(Type objType) {
ConstructorInfo ci = objType.GetConstructor(Type.EmptyTypes);
if (ci == null)
throw new Exception("No parameterless constructor defined for this object.");
return ci.Invoke(null);
}You will need to have special cases for classes that do not implement a default constructor, either by extending the classes with extension methods (if you have .Net 3.0 or above), or by using a try/catch to catch the exception and instantiate those classes yourself. Hope this helps,
Sounds like somebody's got a case of the Mondays -Jeff
-
I'm using the following code to create an instance of a Type: Activator.CreateInstance(propertyType) // Where propertyType is a Type This seems to work fine for the majority of types but when I attempt to use it for a string I get the following error: No parameterless constructor defined for this object. I've been looking high and low for any examples or documentation on the how to do this and haven't been able to find anything. Can anybody help and save my sanity?
I guess the reason is that string class has not got public default constructor. You can try either using other overload of CreateInstance or create an instance of StringBuilder class.
#region signature my articles #endregion
-
I'm using the following code to create an instance of a Type: Activator.CreateInstance(propertyType) // Where propertyType is a Type This seems to work fine for the majority of types but when I attempt to use it for a string I get the following error: No parameterless constructor defined for this object. I've been looking high and low for any examples or documentation on the how to do this and haven't been able to find anything. Can anybody help and save my sanity?
Just wanted to say thanks to everybody that posted. I came across this posting http://groups.google.co.uk/group/microsoft.public.dotnet.languages.csharp/browse_frm/thread/817b411162fa122/9ea3b34cc60bc2a2?hl=en&lnk=st&q=c%23+variables+default+values#9ea3b34cc60bc2a2[^] That goes into default values for variables and using the Activator.CreateInstance. From my set of limited tests so far String is the only Type that I've had any problems with.