首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >属性操作与Action<T>的接口

属性操作与Action<T>的接口
EN

Stack Overflow用户
提问于 2019-11-18 05:00:01
回答 1查看 236关注 0票数 0

我正在尝试创建一个接口,该接口的属性可能是ActionAction<T>作为实际实现。传入的一些方法不需要参数,而有些则需要参数。

在研究和尝试了几种方法之后,下面是我要做的:

代码语言:javascript
运行
复制
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参数作为ActionAction<T>传递,这是创建属性操作的泛型类型列表的问题。

实际的实现是在UI自动化过程中使用的。我正在连接一个AutomationElement,并侦听WindowPattern.WindowOpening事件,这将触发内部foreach循环。这样做的想法是,有一个预定义的窗口列表来监听,以及打开该窗口时调用的方法。这些方法中有些需要参数,而有些则不需要--因此ActionAction<T>

我真的需要在正确的方向上轻轻一推。

编辑:简单的区域包括在内,因为这是当前的代码,我提供它作为我试图到达的地方的上下文。

EN

回答 1

Stack Overflow用户

发布于 2019-11-18 06:35:47

只要在构造GenericAction时知道参数,还可以将对MethodWithParam的调用写为:

代码语言:javascript
运行
复制
Action action = () => MethodWithParam("my parameter");

那么您就不再需要T中的GenericAction了。

代码语言:javascript
运行
复制
string p1 = "parameter one";
var complicatedActions = new List<IGenericAction>()
{
    new GenericAction() { Title = "Foo", Action = MethodWithoutParam },
    new GenericAction() { Title = "Bar", Action = () => MethodWithParam(p1) } 
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58908200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档