我正在尝试创建一个接口,该接口的属性可能是Action或Action<T>作为实际实现。传入的一些方法不需要参数,而有些则需要参数。
在研究和尝试了几种方法之后,下面是我要做的:
using System;
using System.Collections.Generic;
namespace InterfaceProperty
{
class Program
{
static void Main(string[] args)
{
var ClassItems = new List<SomeClass>()
{
new SomeClass() { TitleProp = "Foo" },
new SomeClass() { TitleProp = "Bar" }
};
#region Simple
var SimpleActions = new List<SimpleAction>()
{
new SimpleAction() { Title = "Foo", Action = MethodWithoutParam },
new SimpleAction() { Title = "Bar", Action = MethodWithoutParam }
};
foreach (var item in ClassItems)
{
// In "real life" this would be triggered in an event, and the parent loop would not be required.
foreach (var simpleAction in SimpleActions)
{
if (simpleAction.Title == item.TitleProp)
{
simpleAction.Action();
}
}
}
#endregion
#region Complicated
var ComplicatedActions = new List<IGenericAction<Action>>()
{
new GenericAction<Action>() { Title = "Foo", Action = MethodWithoutParam },
new GenericAction<Action<string>>() { Title = "Bar", Action = MethodWithParam } // fails here
};
foreach (var item in ClassItems)
{
// In "real life" this would be triggered in an event, and the parent loop would not be required.
foreach (var genericAction in ComplicatedActions)
{
if (genericAction.Title == item.TitleProp)
{
genericAction.Action();
}
}
}
#endregion
Console.ReadLine();
}
private static void MethodWithoutParam()
{
Console.WriteLine("Method without a parameter");
}
private static void MethodWithParam(string param)
{
Console.WriteLine($"Method with parameter: {param}");
}
}
public class SomeClass
{
public string TitleProp { get; set; }
}
#region Simple
public class SimpleAction
{
public string Title { get; set; }
public Action Action { get; set; }
}
#endregion
#region Complicated
public interface IGenericAction<T>
{
string Title { get; set; }
T Action { get; set; }
}
public class GenericAction<T> : IGenericAction<T>
{
public string Title { get; set; }
public T Action { get; set; }
}
#endregion
}必须将T参数作为Action或Action<T>传递,这是创建属性操作的泛型类型列表的问题。
实际的实现是在UI自动化过程中使用的。我正在连接一个AutomationElement,并侦听WindowPattern.WindowOpening事件,这将触发内部foreach循环。这样做的想法是,有一个预定义的窗口列表来监听,以及打开该窗口时调用的方法。这些方法中有些需要参数,而有些则不需要--因此Action对Action<T>。
我真的需要在正确的方向上轻轻一推。
编辑:简单的区域包括在内,因为这是当前的代码,我提供它作为我试图到达的地方的上下文。
发布于 2019-11-18 06:35:47
只要在构造GenericAction时知道参数,还可以将对MethodWithParam的调用写为:
Action action = () => MethodWithParam("my parameter");那么您就不再需要T中的GenericAction了。
string p1 = "parameter one";
var complicatedActions = new List<IGenericAction>()
{
new GenericAction() { Title = "Foo", Action = MethodWithoutParam },
new GenericAction() { Title = "Bar", Action = () => MethodWithParam(p1) }
};https://stackoverflow.com/questions/58908200
复制相似问题