如题!
想实现给一个类的某个字段加上一个字定义特性,然后通过反射得到这个字段的名字!

解决方案 »

  1.   

    easy你先通过反射找到你的字段对应的FieldInfo 然后
    Attribute.GetCustomAttribute(yourFieldInfo);
      

  2.   

    请问如何 通过反射得到字段对应的FieldInfo呢?
      

  3.   

    类的字段在反射里就是type的fieldInfos集合里的一个FieldInfo你是不是:public  MyClass 
    {
        [xxxAttribute]
        private string myString;
    }
    想取xxxAttribute???
    如果是这样上面的方法就可以了
      

  4.   

    不是 
    是想得到 字段 myString
      

  5.   

    你先通过反射找到你的字段对应的FieldInfo 的Name属性就是你的字段的名称
      

  6.   

    我问的就是如何得到这个FieldInfo
    ------------------------------------------------------------------------你先通过反射找到你的字段对应的FieldInfo 的Name属性就是你的字段的名称
      

  7.   

    先取出所有的public属性,
    PropertyInfo[] myPropertyInfo = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
    再取得每个属性的 自定义特性GetCustomAttribute
      

  8.   

    不能一次直接通过Type对象取得啊?
    也就是对字段循环 然后判断了?
      

  9.   

    //o 是你的类的实例
    Type t = o.GetType();
    t.GetFields()就是获得 了FieldInfo,你可以用foreach 来找
      

  10.   

    Type type = Type.GetType("xxx");//xxx类名称
    foreach (FieldInfo info in type.GetFields())
                {
                    foreach (Attribute attr in info.GetCustomAttributes(false))
                    {
                          ...//do sth.
                    }
                }
      

  11.   

    EntityBase target = new TestEntity();
                System.Reflection.PropertyInfo []abs = target.GetType().GetProperties();