English 中文(简体)
C# 用作一类外地名称
原标题:C# use string as a class field name
  • 时间:2011-11-18 08:54:14
  •  标签:
  • c#
  • field

如果碎片产生误导,则会发生争执。 我想要做的是利用一种扼杀手段从一个类别中获取价值。 :

class foo
{
    public string field1 {get;set;}
    public string field2 {get;set;}
}

public void run()
{
    //Get all fields in class
    List<string> AllRecordFields = new List<string>();
    Type t = typeof(foo);
    foreach (MemberInfo m in t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
    {
        AllRecordFields.Add(m.Name);
    }

    foo f = new foo();
    foreach(var field in AllRecordFields)
    { 
        //field is a string with the name of the real field in class
        f.field = "foobar";
    }
}

This a really simple example, so the problem is on the line f.field = "foobar"; The field is a string with a name of the real class field what i want to assignt the value to.

最佳回答

http://www.un.org/apps/pressrec/index.html。

public void run()
{
  foo f = new foo();
  Type t = typeof(foo);

  foreach (PropertyInfo info in t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
  {
     info.SetValue(f, "foobar", new object[0]);
  }
}
问题回答

First of all, it is better to use Properties instead of fields. Second your fields are private, can t be accessed from outside foo. You need to declare them as public.

举例来说,你必须利用思考来查阅这些档案。 但这很缓慢,并非很好。 你们可以直接使用这一类别(拥有财产)或我们的接口。

将方法添加到oo类以改变所有特性

   class foo
    {
        public string field1 {get;set;}
        public string field2 { get; set; }

        public void SetValueForAllString( string value)
        {
            var vProperties = this.GetType().GetProperties();
            foreach (var vPropertie in vProperties)
            {
                if (vPropertie.CanWrite 
                    && vPropertie.PropertyType.IsPublic 
                    && vPropertie.PropertyType == typeof(String))
                {
                    vPropertie.SetValue(this, value, null);
                }
            }

        }
    }

    foo f = new foo() { field1 = "field1", field2 = "field2" };
                f.SetValueForAllString("foobar");
                var field1Value = f.field1; //"foobar"

             var field2Value = f.field2; //"foobar"




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

热门标签