リフレクションを直接行うのではなく、C# IL で浅いコピーを実装したいと考えています。
• リフレクションを直接使用する ShallowCopy バージョン:
public T ShallowCopy<T>(T instanceToShallowCopy) where T : class
{
Type type = instanceToShallowCopy.GetType();
T result;
try
{
result = System.Activator.CreateInstance(type) as T;
}
catch (MissingMethodException ex)
{
throw new MissingMethodException("The input instance type should contain default constructor.", ex);
}
catch (AggregateException ex)
{
throw new AggregateException("Activator create instance failed.", ex);
}
foreach (System.Reflection.PropertyInfo propertyInfo in instanceToShallowCopy.GetType().GetProperties())
{
if(propertyInfo.GetIndexParameters().Length == 0)
{
Object value = propertyInfo.GetValue(instanceToShallowCopy);
propertyInfo.SetValue(result, value);
}
}
foreach (System.Reflection.FieldInfo fieldInfo in instanceToShallowCopy.GetType().GetFields())
{
Object value = fieldInfo.GetValue(instanceToShallowCopy);
fieldInfo.SetValue(result, value);
}
return result;
}
• 現在の IL バージョン コードは次のとおりです。
public static class PluginShallowCopyInvoker
{
public delegate Object ShallowCopyDelegate(Object paramType);
/// <summary>
/// Dictionary to cache any params fast constructor invokers, by type. Each entry is a shallowcopy delegates.
/// </summary>
private static ConcurrentDictionary<Type, ShallowCopyDelegate> InvokerCacheDictionary = new ConcurrentDictionary<Type, ShallowCopyDelegate>();
/// <summary>
/// T
/// </summary>
/// <param name="type"></param>
/// <param name="instanceToShallowCopy"></param>
/// <returns></returns>
public static ShallowCopyDelegate GenerateShallowCopyInvoker(Type type, Object instanceToShallowCopy)
{
ShallowCopyDelegate shallowCopyDelegate = null;
if (!InvokerCacheDictionary.TryGetValue(type, out shallowCopyDelegate))
{
shallowCopyDelegate = BuildShallwCopyInvokersForType(type, instanceToShallowCopy);
InvokerCacheDictionary.TryAdd(type, shallowCopyDelegate);
}
return shallowCopyDelegate;
}
/// <summary>
/// This is intent for do shallow copy, but now only create constructor. I don’t know how to do field/property copy.
/// </summary>
/// <param name="type"></param>
/// <param name="instanceToShallowCopy"></param>
/// <returns></returns>
private static ShallowCopyDelegate BuildShallwCopyInvokersForType(Type type, Object instanceToShallowCopy)
{
var invokeMethod = new DynamicMethod("PISC_" + type.FullName, typeof(Object), new Type[]{typeof(Object)}, true);
var ilgen = invokeMethod.GetILGenerator();
var cinfo = type.GetConstructor(Type.EmptyTypes);
if (cinfo == null) return null;
ilgen.Emit(OpCodes.Newobj, cinfo);
ilgen.Emit(OpCodes.Ret);
// I don’t know how to do field/property copy here … hope can got your help ~~~
return (ShallowCopyDelegate)invokeMethod.CreateDelegate(typeof(ShallowCopyDelegate));
}
public static void SetField(Type type, Object instanceToShallowCopy, ref Object newInstance)
{
foreach (System.Reflection.FieldInfo fieldInfo in instanceToShallowCopy.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance))
{
DynamicMethod dm = new DynamicMethod("setter", typeof(void), new Type[] { typeof(object), typeof(object) }, type, true);
ILGenerator setGenerator = dm.GetILGenerator();
setGenerator.Emit(OpCodes.Ldarg_0);
setGenerator.Emit(OpCodes.Unbox, type);
setGenerator.Emit(OpCodes.Ldarg_1);
if (fieldInfo.FieldType.IsValueType)
{
setGenerator.Emit(OpCodes.Unbox_Any, fieldInfo.FieldType);
}
else
{
setGenerator.Emit(OpCodes.Unbox, fieldInfo.FieldType);
}
setGenerator.Emit(OpCodes.Stfld, fieldInfo);
setGenerator.Emit(OpCodes.Ret);
var value = fieldInfo.GetValue(instanceToShallowCopy);
newInstance = dm.Invoke(null, new object[]{newInstance, value});
}
}
インスタンスを構築する方法は知っていますが、他のフィールドやプロパティをコピーする方法はわかりません。
誰でもこれについて私を助けることができますか?
助けていただければ本当にありがたいです?