真是不够聪明:老外写的程序看得头疼?帮位大哥指点一下!
解决方案 »
- 求大神指点,关于用RowDataBound绑定控件后,获取控件值得问题
- linq降序OrderByDescing,不成功
- 请教一个关于水晶报表打印页面大小控制的问题
- 请问如何判断xmlreader读取的文件为空
- #### 关于使用 DEV Express .NET 的网站发布 , 在线等,请高手帮忙, 帮顶有分 ####
- 如何遍历DataTable返回的记录集中的某字段的值?
- 求助textbox驗證問題
- 在线等待,.NET页面上如何控制默认按钮的焦点位置。
- “/testWeb”应用程序中的服务器错误。难度太高,是在搞不懂, 求教各位高手。
- 一个菜鸟问题!在线等待!
- datagrid 导出 excel 问题
- 关于视频网站的制作和参数问题,请各位赐教,谢谢了!
真是不够聪明:老外写的程序看得头疼?帮位大哥指点一下!我的理解:把反射的类结构信息存入静态哈希表中? 但不理解他为什么要这么做?
目的是什么?是深度拷贝还浅拷贝?1、一个拷贝抽象类: [Serializable]
public abstract class CSCopy
{
private static readonly Hashtable objects = new Hashtable(); protected object CreateNewInstance()
{
ConstructorInfo ci = objects[this.GetType()] as ConstructorInfo;
if(ci == null)
{
ci = this.GetType().GetConstructor(new Type[0]);
objects[this.GetType()] = ci;
} return ci.Invoke(null);
} public virtual object Copy()
{
return CreateNewInstance();
}
}
2、实际应用:public class IndexPost : CSCopy
{
/// <summary>
/// 重写拷贝
/// </summary>
/// <returns></returns>
public override object Copy()
{
IndexPost post = base.Copy () as IndexPost; post.BestMatch = this.BestMatch;
post.Body = this.Body;
post.Title = this.Title;
}
。。
}
IndexPost post = base.Copy () as IndexPost;不明白他为什么要这样做呢?是深度拷贝还浅拷贝?按我的理解:如果深度拷贝应该用:IndexPost post = new IndexPost();这不是更加简洁吗?老外为什么要又反射又拷贝,啥意思啊?睬不透老外的奥妙之处?特向各位大哥求教了。
这怎么能算copy呢???这根本是个空对象嘛 ci = this.GetType().GetConstructor(new Type[0]);
objects[this.GetType()] = ci;
就是要把带参的构造信息ci存入hashtable这个ci.Invoke(null); 就能反回对象了
这怎么能算copy呢???这根本是个空对象嘛
我说的这个是在Copy方法呢? 可以实现深度拷贝.....
return ci.Invoke(null); 似呼也是相当消耗资源啊??? 他为什么要这么做, 想不通啊.
{ public T CreateNewObject<T>(this T template) where T : new()
{
return new T();
} private static readonly Dictionary<Type, ConstructorInfo> ConstructorBuffer = new Dictionary<Type, ConstructorInfo>(); public object CreateNewObject(this object template)
{
ConstructorInfo ci;
if (!ConstructorBuffer.TryGetValue(template.GetType(), out ci))
{
Type tt=template.GetType();
ci = tt.GetConstructor(new Type[0]);
if(ci==null)
throw new Exception(string.Format("必须为类型【{0}】设置一个无参数的实例化方法。", tt.FullName); ConstructorBuffer .Add( tt, ci);
}
return ci.Invoke(null);
}
}
{
return new T();
}
只是为了跟“来外”写的代码取得接口格式上的一致。你可以看到,这个参数template根本是多余的。后边一个方法也有点类似,实际上只要给一个Type类型的参数按说就完全可以,不知道为什么一定要传一个对象进来。不过学习和真实的软件开发是不同的,开发时没有必要那么死抠。大项目的开发是靠质量管理手段来保证整体上的质量和进度的,只要达到原先设计的功能就算通过,而不是对每一个程序员的每一个代码都向来是审查学生一样对“标准、效率”之类的东西过分审查。
{ public static T CreateNewObject<T>(this T obj) where T : new()
{
return new T();
} public static void CopyValuesTo<T>(this T source, T target)
{
foreach (FieldInfo fld in typeof(T).GetFields())
fld.SetValue(target, fld.GetValue(source));
foreach (PropertyInfo pro in typeof(T).GetProperties())
pro.SetValue(target, pro.GetValue(source, null), null);
} }
这样,假设我们要把保存了实例化了的TA类型的对象的变量x复制到变量y(仅复制一层属性值),可以调用: TA y=x.CreateNewObject();
x.CopyValuesTo(y);
{ public static T CreateNew<T>(this T obj) where T : new()
{
return new T();
} public static void CopyValuesTo<T>(this T source, T target)
{
foreach (FieldInfo fld in typeof(T).GetFields())
fld.SetValue(target, fld.GetValue(source));
foreach (PropertyInfo pro in typeof(T).GetProperties())
if (pro.CanRead && pro.CanWrite)
pro.SetValue(target, pro.GetValue(source, null), null);
}.......
TO: sp1234 大哥的改进方法确实不错, 把父类独立出来做为一个通用的静态类供调用.....
不错的方法...
/// 处理属性
/// </summary>
/// <param name="baseObject"></param>
/// <returns></returns>
private static string ProcessProperties(object baseObject)
{
// 读取对象属性数组集合
//PropertyInfo[] properties = baseObject.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
PropertyInfo[] properties = baseObject.GetType().GetProperties();
//ArrayList cachekeys = new ArrayList();
// 注意:改用泛型类后比ArrayList要节省一半的时间
System.Collections.Generic.List<string> cachekeys = new System.Collections.Generic.List<string>(); // 遍历属性数组
foreach (PropertyInfo pi in properties)
{
try
{
// 下面逻辑式非常消耗时间
//if (pi.CanRead && !IsIgnored(pi) && !IsIgnoredDefaultValue(pi, pi.GetValue(baseObject, null)))
//{
//cachekeys.Add(string.Format("{0}:{1}", KeyName(pi), pi.GetValue(baseObject, null)));
cachekeys.Add(string.Format("{0}:{1}", pi.Name, pi.GetValue(baseObject, null)));
//}
}
catch //(NullReferenceException nex)
{
}
} //// CS-2533: CacheKey.GetCacheKey() reflection needs a sort order
//// This should use the current culture for comparison
cachekeys.Sort(); //return string.Join("-", (string[])cachekeys.ToArray(typeString));
return string.Join("-", (string[])cachekeys.ToArray());
}
我测试了一下: for (int i = 0; i < 1; i++)
{
Extention.CopyValuesTo<ChenWei>(ll, other);
}就消耗了: Time:0.057