I am loading an xml file that contains both the type and value of an item. EG:
<ListReturnItem itemName="TargetID" itemType="int">23</ListReturnItem>
<ListReturnItem itemName="TargetState" itemType="PublicTargetState">Enabled</ListReturnItem>
PublicTargetState is a predefined unumerated type.
I need to be able to create a new object based on the xml value of itemType, which is a string representation of the type. How can this be done in C# Without using case statements, of course!
In pseudo code, something like:
string sState = "PublicTargetState" // Or xml read itemType
(Type)sState.TypeFromString() TargetState = new (Type)sState.TypeFromString()

Possible to Cast from String representation of Type?
ivan_hristov
Absolutely perfect!!! You are the man...
I knew it was a matter of syntax, I was just struggling to pin it down.
Many thanks!
danielanvar
mohmsdn
Hope this helps.
LoveCode
Thanks for the posting. GetType method worked when I specified the complete namespace.
Tony Maynard-Smith again
GetType takes a string name. As I mentioned above, you need to fully qualify the type, including the namespace, ie
Type type = Type.GetType("System.StringComparison");
doncmorris
GetType does work with value types, however, inner classes are specified differently, you will need to specify the following:
Type type = Type.GetType("Tech.InternalTarget.ListReturnItem+TargetVisibility");
Also make sure that TargetVisibility exists in the same assembly that is calling Type.GetType; otherwise, you also need to specify the assembly.
Deadman Walking
if (p.Length==0)
{ String d = (String) p[0].Invoke(null); }
This should do what you need. The only catch is you need to have the namespace also. And the type should already be loaded (only anb issue if the types are in another assembly).
Steve "The Bug Magnet"
Here are some relevant code snippets...
namespace Tech.InternalTarget { public abstract class ListReturnItem : InternalTarget { public enum TargetVisibility { TVUndefined = 0x00, TVRestrictedState = 0x01, TVPublicTargetState = 0x02, TVPrivateTargetState = 0x03, TVBroadcastState = 0x04 } private TargetVisibility _visibility; public TargetVisibility Visibility { get { return _visibility; } set { _visibility = value; } } } } public abstract class ListRestrictItem : InternalTarget { public enum TargetVisibility { TVUndefined = 0x00, TVUserRestricted = 0x01, TVPublicRestricted = 0x02, TVPrivateRestricted = 0x03, TVBroadcastRestricted = 0x04 } private TargetVisibility _visibility; public TargetVisibility Visibility { get { return _visibility; } set { _visibility = value; } } } } }The following code is in the same assembly. It returns t as null.
Type t = Type.GetType( "Tech.InternalTarget.ListReturnItem.TargetVisibility" ); My current thinking is that GetType doesn't work with value types.anmar
Use the Type.GetType() method:
Type type = Type.GetType(typeName);
However, be aware that you need to fully qualify the type, for example "MyNamespace.MyType", also unless the type lives in mscorlib.dll or in the current executing assembly, you will need to add assembly information, for example "MyNamespace.MyType, MyAssembly".
Once you have the type, you can then pass it to Enum.Parse to parse the value.
Paulredman
Hi, thanks for the reply...
I don't think that code works with an enumerated Type... it was returning null for my types, so I added a type manually with a ToString on it. It just gives the error: Error 2 'PublicTargetState' is a 'type', which is not valid in the given context.
Hoodwinked
GetType returns the same error as above - not valid in the given context. I think it is to do with the enum being defined inside a class.
typeof works, but I can only pass the fully qualified type, and not a text representation.
EG:
This code works: Type t = typeof(PublicTargetState);
But since I only have a string representation of the Type, it is useless.
This code gives a not valid in the given context error at compilation: Type t = Type.GetType(PublicTargetState);
This compiles, but returns a null at runtime: Type t = Type.GetType("PublicTargetState");
Obviously because the context error is preventing the type from being visible. If this worked it would be the solution, as I can pass a string into it rather than hard-coded text.
What I need is the equivalent of:
Type t = typeof("PublicTargetState");
Which of course won't compile - it gives the error: Type expected.
I'm starting to think this is just not possible. But if anyone has any other suggestions, I'll give anything a try!