英文:
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; } = "";
public Action? Method(<T> x) { get; set; } = null;
public MenuItem(string title,Action Method(<T> x)
{
Title = title;
Method = Method(<T> 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("WELCOME");
lager.MenuItems.Add(lager.DictTKey, newMenuItem("MENU",
CreateNewProductItem(controller)));
}
public class Menu
{
public string Title { get; set; } = "";
public int DictTKey { get; set; } = 0;
public Dictionary<int,MenuItem> MenuItems { get; set; } = new Dictionary<int,MenuItem>();
public Menu(string title)
{
Title = title;
}
public void AddMenuItem(string title,Action<RepoController> method)
{
MenuItems.Add(++DictTKey, new MenuItem(title, method));
}
}
public class MenuItem
{
public string Title { get; set; } = "";
public Action<RepoController>? Method { get; set; } = null;
public MenuItem(string title,Action<RepoController> method)
{
Title = title;
Method = method;
}
}
A type conversion happens in CallGUI():
lager.MenuItems.Add(lager.DictTKey, newMenuItem("MENU", CreateNewProductItem(controller)));
CS1503: Argument 2: cannot convert from 'void' to System.Action.<T>'
答案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<T>
{
public string Title { get; set; };
public Action<T>? Method { get; set; };
public MenuItem( string title, Action<T> method )
{
Title = title;
Method = method;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论