0

When i try to use Delegate's Method Property in C# i get this error.

'myDelegate' does not contain a definition for 'Method' and no extension method 'Method' accepting a first argument of type 'myDelegate' could be found (are you missing a using directive or an assembly reference?)

I really don't know the reason why i get this. My program is fairly simple and is based on delegates. Below it's code is given:

public delegate void myDelegate(int x);
public class Program
{
    public void Main(string[] args)
    {
        X x = new X();
        myDelegate d = x.InstanceProgress;
        Console.WriteLine(d.Method);
    }
}

class X
{
   public void InstanceProgress(int percent) => Console.WriteLine(percent);
}

I get error on this line:

Console.WriteLine(d.Method);

See this image below, although i get the proper output but i get the error. I have marked the error with green arrow on the image.

enter image description here

19
  • 5
    Just so you know, its "delegate" not "deligate" (though a deligate sounds somewhat delicious...) Commented Jun 15, 2017 at 19:26
  • What framework are you targeting? Commented Jun 15, 2017 at 19:27
  • Solution DNX SDK version : 1.0.0-beta5 Commented Jun 15, 2017 at 19:29
  • What method from InstanceProgress you want to execute? Obviously, InstanceProgress does not contain Method in its definition. Is this some example from some book or something similar? Commented Jun 15, 2017 at 19:30
  • 1
    So it seems like the appropriate thing to do here is to raise this as a bug with Microsoft Commented Jun 15, 2017 at 22:18

1 Answer 1

1

Looking at OP's screenshot. That looks like a Visual Studio error. And since it lets you build the it's not a true error. I don't get that error in VS2015.

I'd clean the solution and restart visual studio. That should clear it.

Old:

You'll want to do something like this:

X x = new X();
myDeligate d = x.InstanceProgress;
d.Invoke(5);

// or as Rahul pointed out you can simply use
d(5);

Invoke() is what actually calls the method. Until then the delegate is just a pointer to the method that you want to call.

Sign up to request clarification or add additional context in comments.

6 Comments

why not just d(5)?
this is not about invoking a method. .Method is a inbuilt property that provides the name and parameters of the method that will be invoke.
@yogihosting Then what would you expect to happen in your original code?
Then your delegate would need to point to a class instead of a method
Well, though this is a correct answer but certainly OP isn't looking for the same. you might want to see OP's comment in question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.