-1

I have a string that I'm getting from a database that contains a class name, for example: "w_task1". When I fetch the script name from the database, I don't know which script it is so can't write w_task1 t = new w_task1().

Instead I want it to be along the lines of (value of class name) t = new (value of class name)().

Class names I'm trying to open vary from: w_task1.cs - w_task25.

I've tried using the whole:

Type type = Type.GetType("w_task1");
object o = Activator.CreateInstance(type);

...But type is equal to null when the program's ran.

So how do I open the class from the string, without knowing the class type?

1
  • You do not "open the class" as in open a file. You load an assembly and instantiate a class by its runtime type information. C# is not like python or some script language. Commented Mar 22, 2016 at 19:46

2 Answers 2

1

Your answer is here: get assembly by class name

this question as been answered by Marc Gravell

You have to find loaded assembly by your class name (linq will help) then do the reflexion stuff to call your method.

I marked your question as duplicated.

You cannot open a class by the file name. How your CLR can make the link between the file name and the Class inside?

Sign up to request clarification or add additional context in comments.

2 Comments

Was looking for ages, couldn't find that. Thanks! One last question, now I have instantiated the class, how do I call a function from within it?
First please valide the answer :) Second answer directly from Google: stackoverflow.com/questions/6472980/…
1

You need to provide the full namespace and class name.

I would probably do something like this (given that the namespace is always the same and the type is found in the same assembly):

Assembly.GetExecutingAssembly().GetType("MyApp.Scripts." + my_string);

Alternatively, you can

  • store the namespace to the DB
  • search for all types in your assembly(ies) by the name and some interface and probably more criteria.
  • have a dictionary which maps strings to well known type names.

Consider security: where does this data come from? Could a user inject it somehow? An arbitrary class could be loaded and probably executed.

I would use an interface:

IScript o = (IScript)Activator.CreateInstance(type);

Would both reduce the use of more reflection and improve security.

3 Comments

Thanks, I'll experiment with it!
Assembly.GetExecutingAssembly().GetType(string.Format("MyApp.Scripts.{0}", my_string)); IScript o = Activator.CreateInstance(type) as IScript; if (IScript != null) { doSomething} is even cleaner.
@pix: not necessarily. When using string format, don't forget to provide culture info. It's very bloated and unreadable for such simple things. If the case when the type doesn't implement the expected interface is actually a programming error, not something the user can do wrong, I would expect a type cast exception, so no special handling is required.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.