标签:silverlight Windows clone rval System depencyobject tp var using
原文链接:http://www.cnblogs.com/waynebaby/archive/2009/06/17/1504690.html最近玩silverlight 3 需要把一个 plateprojection clone
作为sealed class 不大好clone 于是想了个别的办法
Code
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.Generic;
namespace MMMazeControl
{
public static class DependencyObjectExtention
{
static public T Clone<T>(this T source) where T : DependencyObject
{
DependencyObject proj = (DependencyObject)Activator.CreateInstance(source.GetType());
foreach (var dp in GetTypeProperties(source))
{
proj.SetValue(dp, source.GetValue(dp));
}
return (T)proj;
}
static private System.Collections.Generic.Dictionary<Type, List<DependencyProperty>> PropertyCache = new Dictionary<Type, List<DependencyProperty>>();
static private List<DependencyProperty> GetTypeProperties(DependencyObject obj)
{
var tp = obj.GetType();
List<DependencyProperty> rval = null;
if (!PropertyCache.TryGetValue(tp, out rval))
{
var ls = new List<System.Reflection.FieldInfo>(tp.GetFields());
rval = new List<DependencyProperty>();
for (var i = 0; i < ls.Count; i++)
{
var itm = ls[i];
if (itm.FieldType == typeof(DependencyProperty))
{
rval.Add((DependencyProperty)itm.GetValue(obj));
}
}
PropertyCache.Add(tp, rval);
}
return rval;
}
}
}
用的时候很方便
转载于:https://www.cnblogs.com/waynebaby/archive/2009/06/17/1504690.html
标签:silverlight,Windows,clone,rval,System,depencyobject,tp,var,using 来源: https://blog.csdn.net/weixin_30607659/article/details/96692020
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。