在我维护一个严重违反winforms中的跨线程更新规则的旧应用程序的过程中,我创建了以下扩展方法,以便在我发现它们时快速修复非法调用:
/// <summary>
/// Execute a method on the control's owning thread.
/// </summary>
/// <param name="uiElement">The control that is being updated.</param>
/// <param name="updater">The method that updates uiElement.</param>
/// <param name="forceSynchronous">True to force synchronous execution of
/// updater. False to allow asynchronous execution if the call is marshalled
/// from a non-GUI thread. If the method is called on the GUI thread,
/// execution is always synchronous.</param>
public static void SafeInvoke(this Control uiElement, Action updater, bool forceSynchronous)
{
if …Run Code Online (Sandbox Code Playgroud) .NET框架的ICloneable接口通常提供了一种支持克隆类实例的方法.
但是如果我有多个第三方类,并且不想单独关注每个属性,那么如何有效地克隆这些类的对象呢?(这些类的源代码不可用).有没有办法使用泛型和扩展方法?
我需要的是一个深度克隆,它创建一个包含所有属性和(子)对象的精确副本.
示例:假设您要UserQuery在LinqPad中克隆对象:
void Main()
{
UserQuery uc=this;
var copy=uc.CreateCopy(); // clone it
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是一个CreateCopy()扩展,允许创建一个副本,而不必照顾这个类的细节,因为我没有自己的源UerQuery.
(注意,这UserQuery只是一个示例,用于显示我需要的内容,它也可以是PDF文档类,用户控件类,ADO.NET类或其他任何内容).