有一个类class P 
{
   string a ;
   string b;
   string c;
   ........
}有一文件 file 里面内容为
a="A";
b="B";
c="C";
.....现在要取出文件file里的数据,把对应的数据赋给类P里的变量.怎么做.
变量数很多...

解决方案 »

  1.   

    using System;
    using System.IO;
    using System.Reflection;
    using System.Text.RegularExpressions;public class P 

      string a ; 
      string b; 
      string c; 

    class Test {
        static void Main() {
            P p = new P();
            Type type = typeof(P);
            
            // Fill p with the values stored in a.txt
            foreach (string line in File.ReadAllLines("a.txt")) {
                string[] temp = line.Trim('"', ';').Split('=');
                if(temp != null && temp.Length == 2) {
                    string fieldName = temp[0].Trim();
                    string fieldValue = temp[1];
                    FieldInfo fi = type.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                    if(fi!= null) {
                        fi.SetValue(p,fieldValue);
                    }
                }
            }
            
            // Print fields of p
            foreach (FieldInfo info in type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) {
                Console.WriteLine("p.{0} = {1};", info.Name, info.GetValue(p));
            }        Console.ReadLine();
        }
    }
      

  2.   

    感谢cppfaq ...感谢上面所有回帖者..明天来结
      

  3.   

    还有一种方法,就是引用一下Microsoft.VisualBasic的dll,然后用其的callbynmae:namespace ConsoleApplication1
    {
        class Test
        {
            public string a;
            public string b;
            public string c; 
        }
        
        class Program
        {        static void Main(string[] args)
            { 
                Test t=new Test();
                string[] arr={"a=A","b=B","c=C"}; 
                foreach (string v in arr)
            {
            string[] tmp=v.Split('=');
                    Microsoft.VisualBasic.Interaction.CallByName(t, tmp[0], Microsoft.VisualBasic.CallType.Let, tmp[1]);      
            }
                Console.WriteLine("{0} / {1} / {2}",t.a,t.b,t.c  );
            }     }
    }
      

  4.   


    下面的没戏了,我算路过。
    属性有点麻烦,得用反射。在我的项目中有这样的机制。我提取了部分代码:
    第一个类,作用很简单.
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
    public class ObjectIdentityAttribute : Attribute
    {
    private string m_IdentityPropertyName;

    public ObjectIdentityAttribute(string strIdentityPropertyName)
    {
    this.m_IdentityPropertyName = strIdentityPropertyName;
    } public string IdentityPropertyName
    {
    get
    {
    return m_IdentityPropertyName;
    }
    }
    }
    第2个类是一个abstact Object类,它提供了一个方法:根据一个字符串(就是上面类构造传进去的string),返回属性名=该字符串的属性。不存在就shrow Exception
    我就只提供几个方法:private PropertyInfo GetIdentityProperty()
    {
    Type voTypeConcrete = this.GetType();
      
    ObjectIdentityAttribute attributeIdentity = (ObjectIdentityAttribute) this.GetIdentityAttribute( voTypeConcrete );
    PropertyInfo propertyIdentity = voTypeConcrete.GetProperty( attributeIdentity.IdentityPropertyName ); bool propertyNotFound = ( propertyIdentity == null ); if ( propertyNotFound )//没有找到提示错误信息
    {
    throw new InvalidOperationException( message );
    }

    return propertyIdentity;
    }private ObjectIdentityAttribute GetIdentityAttribute( Type ConcreteVOType )
    {
    object[] attributesProposed = ConcreteVOType.GetCustomAttributes( typeof( ObjectIdentityAttribute ), false );
    bool attributeIsAttached = ( attributesProposed.Length == 1 ); ObjectIdentityAttribute attributeIdentityInfo = null;

    if ( attributeIsAttached )
    {
    attributeIdentityInfo = (ObjectIdentityAttribute) attributesProposed[ 0 ];
    }
    else
    {
    throw new InvalidOperationException( message );
    } return attributeIdentityInfo;
    }