日期:2014-05-18  浏览次数:20895 次

C#中Attribute属性,Attribute属性
C#中Attribute属性,哪位有好的总结没???C#中Attribute一般用在哪些地方,有网址的,多贴几个,谢谢了!


------解决方案--------------------
用于为反射提供元数据信息。
------解决方案--------------------
这东西无法总结,也不要总结。

如同你不会去总结究竟你会用哪些单词组合作为你的变量名,或者.NET库中一共有多少个方法一样。这东西就像天上的星星数不清。
------解决方案--------------------
C# code

 /// <summary>
    /// 属性赋值
    /// </summary>
    /// <typeparam name="T">目标属性</typeparam>
    public static class MyProperty<T>
    {
        public static T PropertyCopy(object source, T target)
        {
            Type sourceType = source.GetType();
            Type targetType = target.GetType();
            PropertyInfo[] pis = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo pi in pis)
            {
                string propertyName = pi.Name;
                PropertyInfo pit = targetType.GetProperty(propertyName);
                if (pit != null)
                {
                    pit.SetValue(target, pi.GetValue(source, null), null);
                }
            }
            return target;
        }
    }