【问题标题】:C#: How to enumerate class with nested classes and with all fieldsC#:如何使用嵌套类和所有字段枚举类
【发布时间】:2013-06-06 20:06:11
【问题描述】:

我有一个大的 .ini 文件。 e.c:

[Globals]
EdgeDkShadowColor = 188 196 218
EdgeFillColor = 244 244 244

[SysMetrics]
ActiveCaption = 207 214 232
Background = 58 110 165
Btnface = 244 244 244
...

[Button.Checkbox]
AccentColorHint = 250 196 88
Bgtype = imagefile
BorderColorHint = 29 82 129
FillColorHint = 33 161 33
...

[Button.Checkbox(Checkeddisabled)]
TextColor = 161 161 146

[Button.Checkbox(Mixeddisabled)]
TextColor = 161 161 146

[Button.Checkbox(Uncheckeddisabled)]
TextColor = 161 161 146

我创建了静态类。我想枚举 .ini 的行并为类参数中的所有字段设置值。

类结构是:

public static class Parameters
{
    public static class Globals
    {
        public static string EdgeDkShadowColor;
        public static string EdgeFillColor;
        ...
    }

    public static class SysMetrics
    {
        public static string ActiveCaption;
        public static string Background;
        public static string Btnface;
        ...
    }

    public static class Button
    {
        public static class Checkbox
        {
            public static string AccentColorHint;
            public static string Bgtype;
            public static string BorderColorHint;
        }

        public static class Checkbox_Checkeddisabled
        {
            public static string TextColor;
        }

        public static class Checkbox_Mixeddisabled
        {
            public static string TextColor;
        }

        public static class Checkbox_Uncheckeddisabled
        {
            public static string TextColor;
        }
        ...

如何正确枚举类中的所有字段并初始化它们以最终获得一个对象:

Parameters.
Globals.
    EdgeDkShadowColor = "188 196 218";
    EdgeFillColor = "244 244 244";
SysMetrics.
    ActiveCaption = "207 214 232"
    Background = "58 110 165"
    Btnface = "244 244 244"
    ...
Button.
    Checkbox.
        AccentColorHint = "250 196 88"
        Bgtype = "imagefile"
        BorderColorHint = "29 82 129"
        ...       etc.

附:

  1. 所有值都是字符串。
  2. '('中的名字被'_'代替了。
  3. 参数名称可以包含字符串“::”。替换为“Ext”。

更新

我找到了这个任务的第一个代码。 我尝试使用此功能: 代码的主要部分是

StringReader str = new StringReader(fileAsString);
string line;
Type curType = null;
while ((line = str.ReadLine()) != null)
{
if (string.IsNullOrEmpty(line) | line.StartsWith(";")) continue;
if (line.Contains('['))
{
    line = line[0] + line[1].ToString().ToUpper() + line.Substring(2);
    var listing = typeof(Parameters).GetNestedTypes().ToList();
    string lineS = line.Trim('[').Trim(']').Trim(')').
                        Replace("(", "_").Replace("::", "Ext").Trim();
    var listingOf = listing.Find(tipe => tipe.Name == lineS);
    curType = listingOf;
}
else
{
    if (curType != null)
    {
        FieldInfo found = curType.GetField(splits[0].Trim(')').Replace("(", "_").Trim());
        if (found != null)
            found.SetValue(null, splits[1].Trim());
    }
}
}

这是有效的,但仅适用于一个级别。这是这段代码的工作结果:

http://postimg.org/image/5s28m4c8h/

【问题讨论】:

  • 您是说要根据从文本文件中读出的内容来填充您创建的类的对象吗?
  • 你有没有尝试过?请发布您最初的尝试以及失败的原因。
  • @p.s.w.g 第一个代码已发布并等待帮助

标签: c# class field nested-class enumerate


【解决方案1】:

这听起来像是一个很好的反思候选者。基本上,您会读取 INI 文件的每一行,并根据“。”跟踪深度。配置中的分隔符,然后尝试根据深度在您的类上找到匹配的属性...如果这样做,请使用该值填充它。我最近用表达式树做了类似的事情......对于每个用“。”分隔的字符串字符,访问具有该名称的属性或字段,然后尝试在该属性上找到下一个...提供代码示例将是相当疯狂的,但这绝对是我处理它的方式...反射,因为模式是您的类的属性和子属性与 INI 文件中的命名约定匹配。

【讨论】:

    【解决方案2】:

    找到解决方案:

    string bytePath = resPath + themesIni;
    
    if (!File.Exists(bytePath))
    {
        if (foundRow != null) bytePath = resPath + @"\" + foundRow.ItemArray[1] + @"\" + themesIni;
    }
    byte[] arr = File.ReadAllBytes(bytePath);
    string fileAsString = new UnicodeEncoding().GetString(arr);
    StringReader str = new StringReader(fileAsString);
    string line;
    Type curType = null;
    Type parentType = null;
    while ((line = str.ReadLine()) != null)
    {
        if (string.IsNullOrEmpty(line) | line.StartsWith(";")) continue;
        if (line.Contains('['))
        {
            line = (line[0] + line[1].ToString().ToUpper() + line.Substring(2))
                .Trim('[').Trim(']').Trim(')').Replace("(", "_").Replace("::", "Ext").Trim();
            string[] splitLines = line.Split('.');
            //splitLines[0] = 
            //    (splitLines[0][0] + splitLines[0][1].ToString().ToUpper() + splitLines[0].Substring(2))
            //    .Trim('[').Trim(']').Trim(')').Replace("(", "_").Replace("::", "Ext").Trim();
            //splitLines[1] = 
            //    (splitLines[1][0] + splitLines[1][1].ToString().ToUpper() + splitLines[1].Substring(2))
            //    .Trim('[').Trim(']').Trim(')').Replace("(", "_").Replace("::", "Ext").Trim();
            if (splitLines.Length > 1)
            {
                if (parentType == null)
                {
                    parentType = typeof(Parameters).GetNestedTypes().ToList()
                        .Find(tipe => tipe.Name == splitLines[0]);
                    List<Type> listing = parentType.GetNestedTypes().ToList();
                    curType = listing.Find(tipe => tipe.Name == splitLines[1]);
                }
                else
                {
                    List<Type> listing = parentType.GetNestedTypes().ToList();
                    curType = listing.Find(tipe => tipe.Name == splitLines[1]);
                }
            }
            else
            {
                parentType = null;
                List<Type> listing = typeof (Parameters).GetNestedTypes().ToList();
                string lineT = line;
                Type listingOf = listing.Find(tipe => tipe.Name == lineT);
                curType = listingOf;
            }
        }
        else
        {
            string[] splits = line.Split('=');
            splits[0] = splits[0].Substring(0, 1).ToUpper() + splits[0].Substring(1);
            if (curType != null)
            {
                FieldInfo found = curType.GetField(splits[0].Trim(')').Replace("(", "_").Trim());
                if (found != null)
                    found.SetValue(null, splits[1].Trim());
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-14
      • 2010-09-18
      • 2019-09-12
      • 2016-03-31
      • 1970-01-01
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      相关资源
      最近更新 更多