tags:

views:

94

answers:

2

I have some logic in a method that operates on a specified type and I'd like to create a generic lambda that encapsulates the logic. This is the spirit of what I'm trying to do:

public void DoSomething()
{
    // ...

    Func<T> GetTypeName = () => T.GetType().Name;

    GetTypeName<string>();
    GetTypeName<DateTime>();
    GetTypeName<int>();

    // ...
}

I know I can pass the type as a parameter or create a generic method but I'm interested if it can just be a generic lambda (So I'm not looking for alternatives). From what I can tell C# 3.0 doesn't support this.

TIA,

m

+1  A: 

This is only possible when your DoSomething method is generic or its class is generic.

Steven
+2  A: 

It is not possible to create a lambda expression which has new generic parameters. You can re-use generic parameters on the containing methods or types but not create new ones.

JaredPar
Yeah, that's what I was thinking, bummer. Thx for the feedback.
Mike OBrien