tags:

views:

53

answers:

3

I'm trying to call

Type.GetType("System.Diagnostics.TraceFilter")

not typeof(System.Diagnostics.TraceFilter)

but the result is always null. Could anyone help me out? How to get class type for this abstract class?

+2  A: 

It's probably because you must provide an assembly-qualified name.

Better, though, would be to use this instead:

typeof(System.Diagnostics.TraceFilter)
siride
The idea is getting Type object of any classes. I would like to use reflection in this case. Its assembly is System. Thus, by default Type.GetType should understand where it is.
Mark Attwood
Can you explain what the more general problem you are trying to solve is? There may be a way to use typeof() and generics instead of strings.
siride
+5  A: 

From the documentation for Type.GetType(string typeName)

The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

The type you are fishing around for (i.e. "System.Diagnostics.TraceFilter") is not in the currently executing assembly or in "Mscorlib.dll", it is in fact in "System.dll". Therefore you have to use the fully qualified assembly name, e.g.:

 Type type = Type.GetType("System.Diagnostics.TraceFilter, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

Alternatively you can use:

 Type type = typeof(System.Diagnostics.TraceFilter);
chibacity
+1  A: 

According to MSDN, Type.GetType(string) requires an assembly-qualified name unless the target type is either in the currently executing assembly or mscorlib.dll. System.Diagnostics.TraceFilter is in System.dll, which means that you must use an assembly-qualified name.

Adam Robinson