好的,我在網上找不到任何東西,所以我最終實現了我自己的。這應該有助於開始編寫某種代理的任何人。
public static class TypeBuilderHelper
{
/// <summary>Creates one constructor for each public constructor in the base class. Each constructor simply
/// forwards its arguments to the base constructor, and matches the base constructor's signature.
/// Supports optional values, and custom attributes on constructors and parameters.
/// Does not support n-ary (variadic) constructors</summary>
public static void CreatePassThroughConstructors(this TypeBuilder builder, Type baseType)
{
foreach (var constructor in baseType.GetConstructors()) {
var parameters = constructor.GetParameters();
if (parameters.Length > 0 && parameters.Last().IsDefined(typeof(ParamArrayAttribute), false)) {
//throw new InvalidOperationException("Variadic constructors are not supported");
continue;
}
var parameterTypes = parameters.Select(p => p.ParameterType).ToArray();
var requiredCustomModifiers = parameters.Select(p => p.GetRequiredCustomModifiers()).ToArray();
var optionalCustomModifiers = parameters.Select(p => p.GetOptionalCustomModifiers()).ToArray();
var ctor = builder.DefineConstructor(MethodAttributes.Public, constructor.CallingConvention, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
for (var i = 0; i < parameters.Length; ++i) {
var parameter = parameters[i];
var parameterBuilder = ctor.DefineParameter(i + 1, parameter.Attributes, parameter.Name);
if (((int)parameter.Attributes & (int)ParameterAttributes.HasDefault) != 0) {
parameterBuilder.SetConstant(parameter.RawDefaultValue);
}
foreach (var attribute in BuildCustomAttributes(parameter.GetCustomAttributesData())) {
parameterBuilder.SetCustomAttribute(attribute);
}
}
foreach (var attribute in BuildCustomAttributes(constructor.GetCustomAttributesData())) {
ctor.SetCustomAttribute(attribute);
}
var emitter = ctor.GetILGenerator();
emitter.Emit(OpCodes.Nop);
// Load `this` and call base constructor with arguments
emitter.Emit(OpCodes.Ldarg_0);
for (var i = 1; i <= parameters.Length; ++i) {
emitter.Emit(OpCodes.Ldarg, i);
}
emitter.Emit(OpCodes.Call, constructor);
emitter.Emit(OpCodes.Ret);
}
}
private static CustomAttributeBuilder[] BuildCustomAttributes(IEnumerable<CustomAttributeData> customAttributes)
{
return customAttributes.Select(attribute => {
var attributeArgs = attribute.ConstructorArguments.Select(a => a.Value).ToArray();
var namedPropertyInfos = attribute.NamedArguments.Select(a => a.MemberInfo).OfType<PropertyInfo>().ToArray();
var namedPropertyValues = attribute.NamedArguments.Where(a => a.MemberInfo is PropertyInfo).Select(a => a.TypedValue.Value).ToArray();
var namedFieldInfos = attribute.NamedArguments.Select(a => a.MemberInfo).OfType<FieldInfo>().ToArray();
var namedFieldValues = attribute.NamedArguments.Where(a => a.MemberInfo is FieldInfo).Select(a => a.TypedValue.Value).ToArray();
return new CustomAttributeBuilder(attribute.Constructor, attributeArgs, namedPropertyInfos, namedPropertyValues, namedFieldInfos, namedFieldValues);
}).ToArray();
}
}
使用(假設你有一個TypeBuilder
對象 - 看到here爲例):
var typeBuilder = ...; // TypeBuilder for a SpaceShipSubClass
typeBuilder.CreatePassThroughConstructors(typeof(SpaceShip));
var subType = typeBuilder.CreateType(); // Woo-hoo, proxy constructors!
正是我一直在尋找!儘管我不得不改變爲'GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)'來獲得那些討厭的'protected'構造函數.. – dlras2
@Dan:很好,我不確定其他人是否需要這個。感謝您添加該關鍵字,我必須手動(錯誤)寫出聲明,然後複製粘貼正文。 – Cameron
這位先生,純金! – Waescher