Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Func<> using base class as parameter

Tags:

c#

I have a bunch of methods which I want to check some metadata of, all of them have different parameters but the parameters all derive from BaseClass.

public void CheckMethod(Func<BaseClass, Object> func)
{
    // Check method metadata here
}

public Object MethodToCheck(DerivedClass foo)
{
    // Whatever...
}

public void Test()
{
    CheckMethod(MethodToCheck);
}

Code fails on CheckMetadata(MethodToCheck), becuase MethodToCheck has a DerivedClass as parameter and not a BaseClass. I've tried using generics:

public void CheckMethod<T>(Func<T, Object> func)

...

CheckMethod<DerivedClass>(MethodToCheck);

I would like the call to CheckMethod to be as little to type as possible and would prefer to only call it with CheckMethod(MethodToCheck). Is this possible?

like image 418
Peter Hedberg Avatar asked May 10 '11 11:05

Peter Hedberg


People also ask

What is Func <> C#?

A Func in C# is a way to define a method in-line that has a return value. There is a similar concept of an Action that doesn't have a return value, but we'll get to that in a sec. The return value's type is always the last generic parameter on the Func 's definition.

What is func t TResult?

Func<T, TResult> defines a function that accepts one parameter (of type T) and returns an object (of type TResult). In your case, if you want a function that takes a Person object and returns a string...you'd want Func<Person, string> which is the equivalent of: string Function(Person p) { return p.Name; }

What is func in C# with example?

C# Func simple example string GetMessage() { return "Hello there!"; } Func<string> sayHello = GetMessage; Console. WriteLine(sayHello()); In the example, we use the Func delegate which has no parameters and returns a single value. This is the function to which we refer with the help of the Func delegate.

What is TResult C#?

Func<TResult> represents a method taking 0 arguments and returning an object of TResult , whereas Action<T> represents a method returning void. You need two different delegates as you can't specify void as a type argument.


2 Answers

Check out the MSDN page on covariance and contravariance. The article seems to suggest that you cannot achieve this in prior versions.

In case you are stuck with an older version, I would suggest that you explore the visitor pattern.

like image 143
Apoorv Avatar answered Sep 19 '22 23:09

Apoorv


How about something like

public void CheckMethod<T>(Func<T, Object> func) where T : BaseClass
like image 41
Rob Avatar answered Sep 20 '22 23:09

Rob