3

I would like to use a function as a variable for me to assign.

Something like this

Class:

public class Func
{
    public string Name { get; set; }
    public string Content { get; set; }
    public Function function { get; set; } //Not sure what type for a func
}

Func:

public void Click_Event(object sender,EventArgs e)
{
//Some code here.
}

List to assign the func:

List<Func> funcList = new List<Func>();
Func func = new Func();
func.Name = "Name";
func.Content = "Hello World";
func.function = Click_Event;  //This is what I really want to do.
funcList.Add(func);

Is there anyway to achieve this?

Edited

Some of the comment below said that Reflection may cause the performance issue, but I use it and doesn't seen any slow issue currently. I use it when I first start the app only.

This is the sample code I use it.

MethodInfo method = this.GetType().GetMethod("FuncName"); //Or using nameof
btnFunc.AddHandler(Button.ClickEvent, Delegate.CreateDelegate(Button.ClickEvent.HandlerType, this, method, false));
Ling
  • 87
  • 1
  • 13
  • 2
    It's called a `delegate` _a pointer to a function_, check [this article](https://www.tutorialspoint.com/csharp/csharp_delegates.htm) – jalsh Feb 13 '20 at 02:53
  • 1
    You are looking for a delegate. Something like `Action` or just use an event handler. – Nkosi Feb 13 '20 at 02:54
  • https://stackoverflow.com/questions/2019402/when-why-to-use-delegates and https://stackoverflow.com/questions/31497/where-do-i-use-delegates – TheGeneral Feb 13 '20 at 03:12
  • It looks like you're doing something wrong. What do you need a `List`. .Net already provides a `Func` class for you. A `Func` has an list backed behind it already (altough the you still may want to use a list because messing with the invocation list can cause issues). But To me it sound like you just want to register a list of funcs with some event handler. – johnny 5 Feb 13 '20 at 17:26

2 Answers2

1

Try using a delegate:

A delegate is a data structure that refers to a static method or to a class instance and an instance method of that class.

https://learn.microsoft.com/en-us/dotnet/api/system.delegate?view=netframework-4.8

Another option to look into would be an Action or Func

Oleksi
  • 12,947
  • 4
  • 56
  • 80
  • I try using Action for this issue, but how can I bind it with btn.Click? It throw me an error 'No overload for Action. Invoke(object,RoutedEventHandler) matches delegate RoutedEventHandler' – Ling Feb 13 '20 at 06:30
1

After a several research, I have found another way to solve this. Save the function name as a string instead of as a Action or delegate

Then use System.Reflection.MethodInfo and btn.AddHandler to use it.

By this way, the function must be public.

Ling
  • 87
  • 1
  • 13
  • 3
    That's really not a great way of doing it, IMO. If the methods you want to use always have the same signature, or one compatible with `EventHandler`, just make it of type `EventHandler`. I'd strongly advise renaming `Func` to something else though, otherwise it will confuse readers who expect `Func` to mean `System.Func`. – Jon Skeet Feb 21 '20 at 07:21
  • Getting your function by Reflection is less performant than accessing them through a delegate. Renaming the function later is also more difficult with Reflection (if you don't use `nameof(): `https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/nameof) – user11909 Feb 21 '20 at 07:33