I have a method in a class in another file that I want to take a dynamic method. I'm having some difficulty negotiating the setup. Any help would be appreciated, thanks!
for example...
File #1:
class DoSomethingClass
{
// define delegate
public delegate void DelegateMethod();
public void Main()
{
DelegateMethod d = Func1;
AnotherClass.CallsDynamicMethod("Test1", d);
d = Func2;
AnotherClass.CallsDynamicMethod("Test2", d);
// will this work?
// AnotherClass.CallsDynamicMethod("Test3", DoSomethingClass.instance.Func3);
}
// candidate methods for delegation
void Func1()
{ Console.WriteLine("calling Func1"); }
void Func2()
{ Console.WriteLine("calling Func2"); }
public void Func3()
{ Console.WriteLine("calling Func3"); }
}
File #2:
class AnotherClass
{
public static void CallsDynamicMethod(string words, DelegateMethod dynamicMethod)
{
Console.WriteLine("this is a " + words + " to call...");
dynamicMethod();
}
}
Hope this answers your problem
class Program
{
static void Method()
{
Console.WriteLine("Method");
}
static void Main(string[] args)
{
Action a = Method;
MyClass.SomeMethod(a);
MyClass.SomeMethod(Method);
Console.ReadLine();
}
}
class MyClass
{
public static void SomeMethod(Action del)
{
del();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.