|
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));
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm still alive...but temporarily moved to work on mobile & web stuffs(j2me/brew/php/flash...something not M$). things have just been very busy, and probably will continue...so don't have chance to maintain & respond. Hope will have time to try to write again, because many ideas with WPF &silver light are waiting. wish me luck
FYI:
- MESHSimPack project(c# library for measuring similarity among concepts of the MESH ontology):
http://sourceforge.net/projects/meshsimpack.