Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if object has method with signature of delegate

Tags:

c#

delegates

How can I check if a object has a method with the same signature of a specific delegate

    public delegate T GetSomething<T>(int aParameter);
    public static void Method<T>(object o, GetSomething<T> gs)
    {
        //check if 'o' has a method with the signature of 'gs'
    }
like image 708
Fabiano Avatar asked Jan 28 '10 09:01

Fabiano


People also ask

What is signature in delegate C#?

A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.

Why delegates why not call methods directly?

If you think of delegates as being similar to interface definitions for a specific type of method, you can start to see why delegates exist. They allow clients of our delegates to ignore all the details of their implementations - even their names!

How do you call a delegate method in C#?

Delegates can be invoke like a normal function or Invoke() method. Multiple methods can be assigned to the delegate using "+" or "+=" operator and removed using "-" or "-=" operator. It is called multicast delegate. If a multicast delegate returns a value then it returns the value from the last assigned target method.

What will happen if a delegate has a non void return type?

When the return type is not void as above in my case it is int. Methods with Int return types are added to the delegate instance and will be executed as per the addition sequence but the variable that is holding the return type value will have the value return from the method that is executed at the end.

Can a delegate refer to any type of object?

A delegate has no knowledge of the instance type aside from the method it wraps, so a delegate can refer to any type of object as long as there is a method on that object that matches the delegate signature. When a delegate is constructed to wrap a static method, it only references the method.

How do you invoke a delegate?

The parameters passed to the delegate by the caller are passed to the method, and the return value, if any, from the method, is returned to the caller by the delegate. This is known as invoking the delegate.

How do I pass a delegate as a parameter?

Because the instantiated delegate is an object, it can be passed as a parameter, or assigned to a property. This allows a method to accept a delegate as a parameter, and call the delegate at some later time. This is known as an asynchronous callback, and is a common method of notifying a caller when a long process has completed.

Can a delegate call a method of a class?

Note: A delegate will call only a method which agrees with its signature and return type. A method can be a static method associated with a class or can be an instance method associated with an object, it doesn’t matter. After declaring a delegate, a delegate object is created with the help of new keyword.


1 Answers

// You may want to tweak the GetMethods for private, static, etc...
foreach (var method in o.GetType().GetMethods(BindingFlags.Public))
{
    var del = Delegate.CreateDelegate(gs.GetType(), method, false);
    if (del != null)
    {
        Console.WriteLine("o has a method that matches the delegate type");
    }
}
like image 162
Darin Dimitrov Avatar answered Oct 09 '22 08:10

Darin Dimitrov