How to declare a constructor with a void Action<T> property as taking in a method with a parameter?

huangapple go评论70阅读模式
英文:

How to declare a constructor with a void Action<T> property as taking in a method with a parameter?

问题

您的代码中存在类型转换错误。要解决此问题,您需要确保在调用 CreateNewProductItem(controller) 时,返回的函数签名与 Action<T> 相匹配。根据错误消息,CreateNewProductItem(controller) 返回 void,而 Action<T> 需要一个带有 RepoController 参数的方法。

您需要修改 CreateNewProductItem(controller),使其返回一个接受 RepoController 参数的方法,以便与 Action<RepoController> 兼容。这可能需要调整 CreateNewProductItem 方法的定义或者创建一个新的方法,以满足这一要求。

请尝试修改 CreateNewProductItem 或创建一个新的方法,以确保返回一个接受 RepoController 参数的方法,然后将其传递给 MenuItem 构造函数。这应该解决类型转换错误。

英文:

Suppose I have the following code:

public class MenuItem
{
    public string Title { get; set; } = &quot;&quot;;
    public Action? Method(&lt;T&gt; x) { get; set; } = null;

    public MenuItem(string title,Action Method(&lt;T&gt; x)
    {
        Title = title;
        Method = Method(&lt;T&gt; x)
    }
}

I want my Method property to take in a parameter of a certain type, be it any given, which can be passed in both the constructor and later invoked (or called by a 3rd method) in another class.

I have tried declaring the Method property with parameter brackets and thought about doing it in the constructor in the same way, which doesn't run. Would using an Action be the right way of doing this? The specific method pertaining to the instance of the object will also be declared in the class supposed to call the Action from the property.

EDIT:
I have tried the solution proposed by @Mike Nakis and the IDE seemingly is able to run the code but produces a type conversion error. Here is the code run in sequential order.

public static void CallGUI()
{
         RepoController controller = new RepoController();
         Menu lager = new Menu(&quot;WELCOME&quot;);
         lager.MenuItems.Add(lager.DictTKey, newMenuItem(&quot;MENU&quot;, 
         CreateNewProductItem(controller)));
}

public class Menu
    {
        
        public string Title { get; set; } = &quot;&quot;;
        public int DictTKey { get; set; } = 0;
        public Dictionary&lt;int,MenuItem&gt; MenuItems { get; set; } = new Dictionary&lt;int,MenuItem&gt;();

        public Menu(string title)
        {
            Title = title;
        }
        public void AddMenuItem(string title,Action&lt;RepoController&gt; method)
        {
            MenuItems.Add(++DictTKey, new MenuItem(title, method));
        }
    }

public class MenuItem
    {
        public string Title { get; set; } = &quot;&quot;;
        public Action&lt;RepoController&gt;? Method { get; set; } = null;

        public MenuItem(string title,Action&lt;RepoController&gt; method)
        {
            Title = title;
            Method = method;
        }
    }

A type conversion happens in CallGUI():

lager.MenuItems.Add(lager.DictTKey, newMenuItem(&quot;MENU&quot;, CreateNewProductItem(controller)));
CS1503: Argument 2: cannot convert from &#39;void&#39; to System.Action.&lt;T&gt;&#39;

答案1

得分: 2

以下是您要翻译的内容:

public class MenuItem<T>
{
    public string Title { get; set; }
    public Action<T>? Method { get; set; }

    public MenuItem(string title, Action<T> method)
    {
        Title = title;
        Method = method;
    }
}
英文:

Would the following work for you?

public class MenuItem&lt;T&gt;
{
    public string Title { get; set; };
    public Action&lt;T&gt;? Method { get; set; };

    public MenuItem( string title, Action&lt;T&gt; method )
    {
        Title = title;
        Method = method;
    }
}

huangapple
  • 本文由 发表于 2023年5月14日 20:27:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定