0

Is it possible to create a class which does not exist with specific name from a dynamic string. I'm asking this because I want to create a class based on some string result.

If it is possible please share some example or some reference link.

I see that my question is confusing, so here is more clarification:

Assume that I have a method/function/procedure which returns a string - MyNewClassName(). I want to create a class with name equal to the result of MyNewClassName(), which is dynamic and also it is a custom name.

EDIT from comment
I'll use it as some kind of debugging, because I'm trying to fix some exception remotely, the problem is that the system does not provide the exception message only the type of the exception. So my idea is to create custom exception named with the result of the actual exception.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
nenito
  • 1,214
  • 6
  • 19
  • 33
  • 1
    Why do you need a dynamicly named class? - and how would you reference it in your code? – Jens Kloster Nov 25 '13 at 12:20
  • 1
    you mean class or instance of a class ? – Sriram Sakthivel Nov 25 '13 at 12:21
  • Take a look at System.Reflection, System.CodeDom.Compiler and Microsoft.CSharp. – Bauss Nov 25 '13 at 12:22
  • @Sriram Sakthivel: I want to create a class dynamically not an instance of a class. I think that all answers below are related to creating of an instance of a class. – nenito Nov 25 '13 at 12:57
  • @Jens Kloster: That's why am I asking about it, because I'm not aware if this is possible. It seems not to be a common practice if it is possible. – nenito Nov 25 '13 at 13:00
  • @nenito I dont think it *is* possible. I was curious about your requirment for wanting a dynamicly named class :) and how you intended to use it in your code – Jens Kloster Nov 25 '13 at 13:05
  • Post some sample string. and you can compile a DLL or Exe, It is not clear what you meant by create a class? – Sriram Sakthivel Nov 25 '13 at 13:12
  • @Jens Kloster: I'll use it as some kind of debugging, because I'm trying to fix some exception remotely, the problem is that the system does not provide the exception message only the type of the exception. So my idea is to create custom exception named with the result of the actual exception. – nenito Nov 25 '13 at 13:14
  • 2
    See for instance http://stackoverflow.com/q/3862226/121309 – Hans Kesting Nov 25 '13 at 13:15
  • @nenito ok :) I think you should mention that in your question - becuase a better solution (than dynamic creating a class) might be found. – Jens Kloster Nov 25 '13 at 13:19
  • @Hans Kesting: Vielen Dank!! – nenito Nov 25 '13 at 13:19
  • Few weeks ago I've participated to a lecture about JVM & opcodes, now it seems that this is related to the same, but concerning C#. :) – nenito Nov 25 '13 at 13:32

2 Answers2

2

If you have the classes assembly qualified full name in a string (obtained from whereever, the user can type it in or something), you can do the following:

var name = "MyNameSpace.MyType, MyAssembly";
var type = Type.GetType(name);
var instance = Activator.CreateInstance(type);

This assumes the assembly you specify is already loaded.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
1

You can get an instance of a type by using Activator.CreateInstance(type) . for getting type by type name see here: Get Type by Name

Community
  • 1
  • 1
Yaugen Vlasau
  • 2,148
  • 1
  • 17
  • 38
  • Oh man.. +1 for referring my answer.. But if you feel question is duplicate, vote to close.. or flag it.. That's what am going to do.. – Sriram Sakthivel Nov 25 '13 at 12:27