⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 copyable.cs

📁 In the previous article, we presented an approach for capturing similarity between words that was co
💻 CS
字号:
using System;
using System.Collections;
using System.Reflection;
using System.ComponentModel;
using System.Windows.Forms;

namespace UDDI_Explorer
{
	public interface ICopyable: ICloneable
	{
		void CopyFrom(object ptrFrom);
	}
	/// <summary>
	/// Summary description for BaseObject.
	/// </summary>
	[Serializable]
	public class Copyable : ICopyable
	{
		public Copyable()
		{
		}

		public object Clone()
		{
			object result = Activator.CreateInstance(this.GetType());
			(result as ICopyable).CopyFrom(this);
			return result;
		}

		public void CopyFrom(object ptrFrom)
		{
			Copy(ptrFrom, this);
		}	
		
		private static bool HasNonParameterContructor(Type tfType)
		{
			bool bRet = false;
			ConstructorInfo []colInfos = tfType.GetConstructors();
			for(int i = 0; i< colInfos.Length; i++)
			{
				ParameterInfo []colParameters = colInfos[i].GetParameters();
				if(colParameters.Length==0)
				{
					bRet = true;
					break;
				}
			}
			return bRet;
		}

		public static void Copy(object ptrFrom,object ptrTo)
		{
			Type fromType = ptrFrom.GetType();
			Type toType = ptrTo.GetType();

			if(!(toType.GetInterface("ICloneable") == typeof(System.ICloneable)||toType.GetInterface("ICopyable") == typeof(UDDI_Explorer.ICopyable)) 
				|| !(fromType.GetInterface("ICopyable") == typeof(UDDI_Explorer.ICopyable)||fromType.GetInterface("ICloneable") == typeof(System.ICloneable))
				)
				throw new Exception("CopyFrom not allows different types");
			ArrayList fromFields = new ArrayList();
			Type currType = fromType;
			do
			{
				FieldInfo []Fields = currType.GetFields(
					BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.GetField|BindingFlags.Instance);
				fromFields.AddRange(Fields);
				currType = currType.BaseType;
			}while(currType != null);

			for(int i=0; i<fromFields.Count; i++)
			{
				FieldInfo fromField = fromFields[i] as FieldInfo;
				Type currToType = toType;
				FieldInfo toField = null;
				do
				{
					toField = currToType.GetField(fromField.Name,
						BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.GetField|BindingFlags.Instance);
					currToType = currToType.BaseType;

				}while(toField == null || currToType == null);

				Type tfType = toField.FieldType;
				object fromValue = fromField.GetValue(ptrFrom);
				object toValue = toField.GetValue(ptrTo);
				if(null!=fromValue&&HasNonParameterContructor(tfType))
				{
					MethodInfo mi;
					if(tfType.GetInterface("ICopyable") == typeof(UDDI_Explorer.ICopyable)||tfType.IsSubclassOf(typeof(Copyable)))
					{
						mi = tfType.GetMethod("CopyFrom");
						object []parameters = {fromValue};
						if(toValue == null)
						{
							toValue = Activator.CreateInstance(tfType);
							toField.SetValue(ptrTo,toValue);
						}    
						mi.Invoke(toValue, parameters);
					}
					else if(tfType.GetInterface("ICloneable") == typeof(System.ICloneable))
					{
						mi = tfType.GetMethod("Clone");	
						object []parameters = {};
						if(toValue == null)
						{
							toValue = Activator.CreateInstance(tfType);
							toField.SetValue(ptrTo,toValue);
						}    
						toValue = mi.Invoke(fromValue, parameters);
						toField.SetValue(ptrTo, toValue);
					}
					else
					{
						toField.SetValue(ptrTo, fromField.GetValue(ptrFrom));
					}
				}
				else
				{
					toField.SetValue(ptrTo, fromField.GetValue(ptrFrom));
				}
			}
		}	
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -