Peter, where are you getting the function pointer to check against the
particular delegate? Or, are you dealing stricly with a MethodInfo object
and a delegate?
Usually, you let the language do a compile-time check for you:
private void MyOtherMethod()
{
}
private void MyMethod(MethodInvoker dgate)
{
}
//...
MyMethod(new MethodInvoker(MyOtherMethod));
On Thu, 22 Jun 2006 15:44:24 +0200, Peter van der Weerd
<[EMAIL PROTECTED]> wrote:
>Barry,
>
>Tnanx, You're right of course.
>But I need information about the *signature* of functions a delegate can
>bind to.
>
>Piewie.
>
>----- Original Message -----
>From: "Barry Kelly" <[EMAIL PROTECTED]>
>To: <[email protected]>
>Sent: Thursday, June 22, 2006 3:15 PM
>Subject: Re: [ADVANCED-DOTNET] Getting MemberInfo from a delegate
>
>
>Peter van der Weerd <[EMAIL PROTECTED]> wrote:
>
>> Suppose I typed the following delegate:
>> public delegate void MyDelegate(int i);
>>
>> This delegate is only bindable to functions getting an int as a
parameter.
>
>Delegates are actually more flexible than the C# language permits. The
>delegate above is also bindable to static methods taking both an object
>and an int. The first parameter gets the value that was passed to
>CreateDelegate as the object instance (and it can be any object).
>
>---8<---
>using System;
>using System.Reflection;
>
>class App
>{
> delegate void Foo(int x);
>
> static void Main(string[] args)
> {
> MethodInfo myFoo = typeof(App).GetMethod("MyFoo",
> BindingFlags.Static | BindingFlags.NonPublic);
> Foo foo = (Foo) Delegate.CreateDelegate(typeof(Foo), null,
> myFoo);
> foo(42);
> }
>
> static void MyFoo(object obj, int x)
> {
> Console.WriteLine("obj is null: " + (obj == null));
> Console.WriteLine("x: " + x);
> }
>}
>--->8---
>
>> My question is: how can I get information to what type of functions a
>> delegate can bind to.
>> Basically I want to write a function which accepts a typeof(MyDelegate)
>> and
>> is able to check if the MemberInfo of a method is bindable to this
>> delegate.
>
>You've got all the information in the MethodInfo (parameter and return
>types) and in the delegate (find the Invoke method on the delegate type
>and look at its parameter and return types). The rules:
>
>* Return type on the method to bind can be covariant, or more derived
>than the original in the delegate.
>
>* Parameter types on the method to bind can be contravariant, or less
>derived than the original in the delegate.
>
>* Value types can't be coerced to reference types, so co/contravariance
>doesn't apply between int and object, for example. In other words,
>delegate dispatch never performs boxing or unboxing after the arguments
>have been converted to match the declared parameter types on the
>delegate.
>
>* A static method can optionally accept a reference argument from the
>delegate's "Target" property as the value of the first parameter. The
>value of the argument to CreateDelegate must be assignable to variables
>of the declared type of the first parameter if you use this. Again, the
>value passed to CreateDelegate may be a value type, but the parameter
>cannot: it must be a reference type.
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com