0

Possible Duplicate:
How to use reflection to call generic Method?

My actual code looks like this:

Myclass component = go.GetComponent<Myclass>();

I would like to make it more dynamic using reflections, so I tried the following

Type component = Type.GetType("Myclass");
object componentInstance = Activator.CreateInstance(component);
componentInstance = go.GetComponent<component>();

For the last line I get the following error

The type or namespace name 'component' could not be found (are you missing a using directive or an assembly reference?)

Is there way to make this work?

Community
  • 1
  • 1
kumar
  • 8,207
  • 20
  • 85
  • 176

1 Answers1

1

Check this post:

How to dynamically create generic C# object using reflection?

Shows exactly what you need.

Community
  • 1
  • 1
gustavodidomenico
  • 4,640
  • 1
  • 34
  • 50