-1

I have this method that I want to use to return an Object that i have created by passing in the type name:

public object GetObjectType(object objectTypeName)
{
    Type objecType = objectTypeName.GetType();
    return Activator.CreateInstance(objecType);
}

When I do this:

var a =  GetObjectType("Person");

I get: No parameterless constructor defined for this object.

Im not too sure what this CreateInstance does so im flying blind here. Do I need to add something to my class which looks like this:

public class Person
{
    public string Name {get; set;}
}
Mike Barnes
  • 4,217
  • 18
  • 40
  • 64

4 Answers4

5

In fact you are trying to create new instance of String type, which indeed does not have a parameterless contructor.

public object GetObjectType(object objectTypeName)
{
    Type objecType = objectTypeName.GetType(); // objecType is String here
    return Activator.CreateInstance(objecType); // creation of String fails
}

What you might really want to do is this:

public object GetObjectType(string objectTypeName)
{
    Type objecType = Type.GetType(objectTypeName);
    return Activator.CreateInstance(objecType);
}
abto
  • 1,583
  • 1
  • 12
  • 31
Andrei
  • 55,890
  • 9
  • 87
  • 108
  • `Type objecType = Type.GetType(objectTypeName);` gives null? – Mike Barnes Jan 09 '14 at 11:28
  • @MikeBarnes, that is a different story - you might need a fully qualified name (if in different namespace), or an assembly (if in different assembly). Check out [this thread](http://stackoverflow.com/questions/223952/c-sharp-create-an-instance-of-a-class-from-a-string) for more options, something make me think that second answer migth be your best bet. At least now you have the error while creating the actual type you need. – Andrei Jan 09 '14 at 11:30
  • @Andrei, your solution won't compile, because `Type.GetType(typeName)` needs a string as parameter, not an object. – abto Jan 09 '14 at 11:37
  • @abto, you are right, sorry, missed that first time. Can you please make the edit again? – Andrei Jan 09 '14 at 11:43
3

Try to add a parameterless constructor:

public class Person
{
    public string Name {get; set;}

    public Person() {} // Parameterless constructor
}
Christoph Brückmann
  • 1,373
  • 1
  • 23
  • 41
  • It might be worth noting *why* `Activator.CreateInstance` might need a parameterless constructor. – Timothy Groote Jan 09 '14 at 11:18
  • 7
    FYI - Just tested it, you don't need to explicitly have a parameterless constructor unless you *also* have a parameterized one. Having no constructor counts as having a paramless one. – Obsidian Phoenix Jan 09 '14 at 11:19
0

It seems that your are trying to create object without passing arguments to available parameterised constructor u have. Try this:

public object GetObjectType(object objectTypeName)
{
    Type objecType = objectTypeName.GetType();
    //ur code works fine if do not have parameterised constructor
    return Activator.CreateInstance(objecType,"(do not forget to pass arguments)");
}
  • 1
    This would lead to an instance of type `string` with "(do not forget to pass arguments)". – abto Jan 09 '14 at 11:54
0

Here is how I have solved the problem:

Type objectType = Type.GetType("MyProject.Models.Person", true);
Object objectTypeInstance = (Activator.CreateInstance(objectType));

I can now retrieve my objects

Mike Barnes
  • 4,217
  • 18
  • 40
  • 64