English 中文(简体)
如何在另一类不同情况下通过每组别的不同类别,并采用特定方法
原标题:How to loop through each discrete instance of a class within another instance of a different class and call a specific method

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();

等等。

I would like to do something like this instead:

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

每个成员都属于不同类别,他们共同拥有的唯一东西是,他们都有一个称为“法律”的真空方法。 我不能把所有方法结合起来,采用一种巨大的教义方法。

最佳回答

假设你所谈论的是财产,你可以做以下非常简单的编码:

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

但正如已经提到的那样,这似乎与你的架构一样。

问题回答

您必须在此使用接口:

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();
    }
}

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

如果你知道你已经采用的方法,就可将这些方法列入<条码>行动阵列:

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

foreach(var method in methods)
    method();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签