views:

714

answers:

5

Good afternoon,

I am somewhat lost and maybe can give me a quick push in the right direction:

I have the System.Type of a certain object but need to pass that over as a Type Parameter T to another method... is that somehow possible? Or am I lost in the bigger picture there?

Cheers and thanks, -J

A: 

You can do this by using reflection.

Don't you want to change your design instead (to avoid reflaction)? So you could pass your type as parameter:

DoSomething(yourObject.GetTaype());
Koistya Navin
A: 

Consider using an alternative design. Generic types are intended to be statically resolved at compile-time.

Still, it's possible to do. You need to resort to reflection:

public class TypeReporter<T> {
    public TypeReporter() {
        Console.WriteLine("this type is: {0}", typeof(T));
    }

    // other methods
}

public class StackOverflow694683 {
    static void Main() {
        string n = "..."; // Pick a type, such as System.String.
        Type arg = Type.GetType(n);
        Type genericClass = typeof(TypeReporter<>);
        Type createdClass = genericClass.MakeGenericType(arg);

        // Result is of TypeReporter<T> type. Invoke desired methods.
        object result = Activator.CreateInstance(createdClass);
    }
}
John Feminella
+1  A: 

This is not possible. If you think about it, a Type Parameter is resolved at compile time, whereas the System.Type is resolved by reflection at runtime.

Now, having said it's impossible, it is possible by using reflection. If you create the class with reflection you can pass in a System.Type as a parameter, but it's probably just worth redesigning whatever it is you're trying to do.

EDIT: Here's some ideas for a redesign.

Where does the System.Type come from? Could you pass it in as a type parameter itself so it can be passed through?

If not, could you make an adapter that handles the known types that will be used? Perhaps a switch statement that converts from the System.Type to the right sort of generic call? Anything is faster than reflection.

Cameron MacFarland
+1  A: 

What you're asking to do is not possible in .Net. Type parameters are a compile type operation in .Net (C# and VB). A System.Type instance though is a runtime construct. Any querying about the real type behind System.Type must occur at Runtime. Thus the solutions are not compatible.

For Example:

public void DoSomething<T>(T value) {
  // Do something with a value of type T
}

public Example1() {

  DoSomething(42);  // Calls DoSomething<int>
  Type t1 = typeof(int);
  DoSomething(t1);  // Calls DoSomething<Type>

  object o1 = 42;
  Type t2 = o1.GetType();
  DoSomething(???)  // No way to call DoSomething<int> here without some
                    // wild reflection because the call to DoSomething is
                    // established at compile type where t2 is established
                    // at runtime
}
JaredPar
A: 

What you usually do is to have overloads for the generic method specifically for these kinds of operations. E.g.:

Load<T>();
Load(Type type);
Rashack
That's what -I- do usually, but the method is a 3rd party one and it only accepts the SomeMethod<T>(); structure... that's why I was asking..
Jörg B.