如何在不同类的另一个实例中循环访问一个类的每个离散实例并调用特定方法

发布于 2024-12-10 14:22:03 字数 491 浏览 0 评论 0原文

我有一个类,它使用各种方法和其他类作为其成员。在其中一个方法中,它执行如下操作:

MyObject.member1.doStuff();
MyObject.member2.doStuff();
MyObject.member3.doStuff();
MyObject.member4.doStuff();

等等。

我想做这样的操作:

foreach Member in MyObject
{
    foreach Method in Member
        {
            if (Method.Name == "doStuff")
                { Method.Invoke() }
        }
}

每个成员都属于不同的类类型,它们唯一的共同点是它们都有一个名为 doStuff 的 void 方法()。我无法将所有方法合并到一个巨大的 doStuff() 方法中。

I have a class that uses a variety of methods and other classes as its members. In one of these methods, it does something like this:

MyObject.member1.doStuff();
MyObject.member2.doStuff();
MyObject.member3.doStuff();
MyObject.member4.doStuff();

etc.

I would like to do something like this instead:

foreach Member in MyObject
{
    foreach Method in Member
        {
            if (Method.Name == "doStuff")
                { Method.Invoke() }
        }
}

Each member is of a different class type, the only thing they have in common is they will all have a void method called doStuff(). I cannot combine all methods into one giant doStuff() method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-12-17 14:22:03

你必须在这里使用接口:

static void Main(string[] args)
{

    MyClass mc = new MyClass();
    mc.doStuff();
}

interface IMember
{
    void doStuff();
}

class Member1 : IMember
{
    public void doStuff()
    {
        Console.WriteLine("Member1");
    }
}

class Member2 : IMember
{
    public void doStuff()
    {
        Console.WriteLine("Member2");
    }
}

class Member3 : IMember
{
    public void doStuff()
    {
        Console.WriteLine("Member3");
    }
}

class Member4 : IMember
{
    public void doStuff()
    {
        Console.WriteLine("Member4");
    }
}

class MyClass
{
    public Member1 member1 = new Member1();
    public Member2 member2 = new Member2();
    public Member3 member3 = new Member3();
    public Member4 member4 = new Member4();

    public void doStuff()
    {
        IMember[] members = { member1, member2, member3, member4 };
        foreach (IMember member in members)
            member.doStuff();
    }
}

you have to use interface here:

static void Main(string[] args)
{

    MyClass mc = new MyClass();
    mc.doStuff();
}

interface IMember
{
    void doStuff();
}

class Member1 : IMember
{
    public void doStuff()
    {
        Console.WriteLine("Member1");
    }
}

class Member2 : IMember
{
    public void doStuff()
    {
        Console.WriteLine("Member2");
    }
}

class Member3 : IMember
{
    public void doStuff()
    {
        Console.WriteLine("Member3");
    }
}

class Member4 : IMember
{
    public void doStuff()
    {
        Console.WriteLine("Member4");
    }
}

class MyClass
{
    public Member1 member1 = new Member1();
    public Member2 member2 = new Member2();
    public Member3 member3 = new Member3();
    public Member4 member4 = new Member4();

    public void doStuff()
    {
        IMember[] members = { member1, member2, member3, member4 };
        foreach (IMember member in members)
            member.doStuff();
    }
}
冷清清 2024-12-17 14:22:03

假设您正在谈论的成员是属性,您可以执行以下非常丑陋的代码:

foreach (var property in MyObject.GetType().GetProperties()
    .Select(pi => pi.GetValue(MyObject, null)))
{
    property.GetType().GetMethod("doStuff").Invoke(property, null);
}

但正如已经提到的,这似乎是您的体系结构的问题。

Assuming the members you are talking about are properties, you can do the following very ugly code:

foreach (var property in MyObject.GetType().GetProperties()
    .Select(pi => pi.GetValue(MyObject, null)))
{
    property.GetType().GetMethod("doStuff").Invoke(property, null);
}

But as has been mentioned, it seems like a problem with your architecture.

四叶草在未来唯美盛开 2024-12-17 14:22:03

如果您必须使用反射来对此类属性进行方法调用,则意味着您的代码很可能设计不当。

如果您已经知道这些方法,则可以将它们放入 Action 数组中:

Action [] methods= { MyObject.member1.doStuff, MyObject.member2.doStuff, 
                     MyObject.member3.doStuff, MyObject.member4.doStuff};

foreach(var method in methods)
    method();

If you have to use reflection for method calls on properties like that it means your code is most likely ill-designed.

If you know the methods already you can just put them into an Action array though:

Action [] methods= { MyObject.member1.doStuff, MyObject.member2.doStuff, 
                     MyObject.member3.doStuff, MyObject.member4.doStuff};

foreach(var method in methods)
    method();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文