0

Can a string be used to declare a type in C#?

Instead of:

MyType foo = bar;

You would have a string of "MyType".

string typeString  = "MyType";
TypeOftypeString foo = bar;

Obviously I don't know the correct way to do this. So the above example is just a way to convey what I am thinking.

In Unity3D, I can set a string to be a specific value before runtime. Can I use that string to define a type? Even if this ends up being an illogical pursuit in my code, is this possible? If so, how do you go about it?

Edit: To Expand on what I said. MyType is a class within the application. I also have MyOtherType and MyDifferantType. I want to be able to have that string be "MyType" or "MyOtherType" and then assign an already created instance of that object to that field. I'm trying to avoid a really long if-else chain or switch() statement where I make a case for each type that the object may encounter, even if that type is pre-determined when the string is set before runtime.

If this seems like a fools errand, let me know.

Douglas Gaskell
  • 9,017
  • 9
  • 71
  • 128
  • 1
    TypeOftypeString will always be string, did you mean typeString value? – Christo S. Christov Mar 28 '15 at 21:37
  • 3
    you can use `Activator.CreateInstance`. – Daniel A. White Mar 28 '15 at 21:38
  • @DanielA.White He wants to create a new type at runtime, not instantiate an already created one – Christo S. Christov Mar 28 '15 at 21:39
  • 1
    TypeOftypeString is an example of what I am trying to convey. It's not a functional example. "MyType" is already created within the application, I just want to create or assign a new field of that type dynamically at runtime based on my string. – Douglas Gaskell Mar 28 '15 at 21:44
  • Aha, then it is my mistake, please refer to Daniel's comment – Christo S. Christov Mar 28 '15 at 21:46
  • Perhaps [Generics](https://msdn.microsoft.com/en-us/library/ms172192%28v=vs.110%29.aspx) would solve your problem? It's the most common replacement for "types-as-strings". – bzlm Mar 28 '15 at 21:49
  • To Expand on what I said. MyType is a class within the application. I also have MyOtherType and MyDifferantType. I want to be able to have that string be "MyType" or "MyOtherType" and then assign an already created instance of that object to that field. I'm trying to avoid a really long if-else chain or switch() statement where I make a case for each type that the object may encounter, even if that type is pre-determined when the string is set before runtime. If this seems like a fools adventure, let me know. – Douglas Gaskell Mar 28 '15 at 21:52
  • @bzlm: Absolutely no relation. – Jon Mar 28 '15 at 21:52
  • 1
    @douglasg14b: Fool's adventure. You simply need to have a common interface for your types, then the only place where you need a switch or equivalent is the place where you create the instance. – Jon Mar 28 '15 at 21:54
  • 1
    Gotcha, thanks for the feedback. I must have become tunnel-visioned on the problem and completely forgot about my use of interfaces. How do I close a topic without deleting it? – Douglas Gaskell Mar 28 '15 at 21:56
  • I probably haven't understood your question, but in case I have, would it help you if you searched the type by name (`Type.GetType`), searched its fields by type (`Type.GetFields`), found the one that matches what you want, and assigned your value to that field of your target object (`FieldInfo.SetValue`)? – Theodoros Chatzigiannakis Mar 28 '15 at 22:06
  • [c# instantiate class from string](http://stackoverflow.com/questions/2247598/c-sharp-instantiate-class-from-string) – CodeCaster Mar 28 '15 at 22:16

2 Answers2

2

This should be a solution:

var yourType= "yourType";
var theType = TheType.GetType(yourType, true);
var variable = (yourTypeCast)Activator.CreateInstance(yourType);
1

If I understood correctly, you want to instantiate a class based on a name from a string. This is one way to do this.

var typeName = "someType";
var type = Type.GetType(typeName, true);
var instanceOfType = (someInterfaceCast)Activator.CreateInstance(type); //I like to use an interface cast here

Update: Like some comments mentioned, if you can avoid this path it is best to do so.

Thufir Hawat
  • 456
  • 5
  • 15
  • If you're using a string to instantiate a type, then you don't know its type. At best you can say it's an `object`. `someInterfaceCast`? Where is that info meant to come from? – spender Mar 28 '15 at 23:51
  • "someInterfaceCast" is an Interface. This would allow you to expose certain methods that you would expect to find inside of the instantiated class. That info does not come from anywhere, it is simply your expectation of what methods/properties the instantiated class will have. – Thufir Hawat Mar 29 '15 at 17:04