English 中文(简体)
我怎么能够加入像这样一类的班子,这样我就能够放弃 lo。
原标题:How can I add to a class like this so I can foreach loop?
  • 时间:2010-11-12 21:22:23
  •  标签:
  • c#
  • c#-4.0

我想能够穿过简单类别的所有扼杀,即:

FormDebugMetrics debugMetrics = new FormDebugMetrics();
foreach(string d in debugMetrics)
    {
        if (!string.IsNullOrEmpty(d) {..}
    }

我的班子也一样(对格特埃计算器没有公开定义):

 public class FormDebugMetrics
    {
        public string DebugMetrics_Top { get; set; }
        public string DebugMetrics_Bottom { get; set; }
        public string DebugMetrics_Bottom { get; set; }
    }

如何做到这一点? 我是否增加了一个统计员?

问题回答

这里有一条方式:

public class FormDebugMetrics : IEnumerable<string> {
    // details elided

    public string DebugMetrics_Top { get; set; }
    public string DebugMetrics_Middle { get; set; }
    public string DebugMetrics_Bottom { get; set; }

    public IEnumerator<string> GetEnumerator() {
        yield return this.DebugMetrics_Top;
        yield return this.DebugMetrics_Middle;
        yield return this.DebugMetrics_Bottom;
    }

    IEnumerator IEnumerator.GetEnumerator() {
        return GetEnumerator();
    }
}

尽管如此,我对你的设计表示怀疑。 不清楚它是否适当。

你们要么需要思考,要么采取不同的做法。 我研究的是使用假肢,而不是使用反思。

不能。 至少不简单。 您可以添加一个计算方法,或将其归入另一个类别,而后者确实知道如何这样做。

你也可以通过反省或C# 4.0动态特征来做到这一点,但我真的要警惕。





相关问题
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. ...

热门标签