//示例类
    public class TestA
    {
        [XmlAnyElement(Name="TestAttributeName")]
        public string TestAProperty { get; set; }
    }
    public class TestB
    {
        [TestOtherAttribute(Other="TestAttributeName")]
        public string TestBProperty { get; set; }
    }
//现在要获取TestA、TestB以及其他更多类的某个属性的特性TestAProperty 的值
//如:TestA->TestAProperty->XmlAnyElement->Name的值;
//TestB->TestBProperty->TestOtherAttribute->Other的值;
//注意:
//类是匿名的,属性也是匿名的,特性同样匿名//目的:
//实现下面的方法
        public static void XConvert<TSource,TTarget>(TSource objA)
        {
            var typeA = objA.GetType();
            var ps1 = typeA.GetProperties();
            foreach (PropertyInfo p1 in ps1)
            {
                var attA = p1.GetCustomAttributes(false);
                if (attA.Length > 0)
                {
                    var attType = attA.GetType();
                    var ap1 = attType.GetProperties();
                    foreach (PropertyInfo ap in ap1)
                    {                    }
                }
            }
            var res = System.Activator.CreateInstance(typeof(TTarget));
            //return res;
        }求教育!求指点!求讨论!

解决方案 »

  1.   

    其实我是来送分的!
    ap是不可以访问FullName                    var attType = attA[0].GetType();
                        var ap1 = attType.GetProperties();
                        foreach (PropertyInfo ap in ap1)
                        {
                            var obj=ap.GetValue(attA[0], null);
                            if(obj!=null&&!string.IsNullOrEmpty(obj.ToString()))
                            valuesA.Add(obj.ToString());
                        }