/// <summary> /// Creates a propxy <see cref="Type" /> for the interface of <typeparamref name="T" />. /// </summary> /// <param name="modBuilder">The module builder to use.</param> /// <param name="proxyTypeNamePrefixProvider">The logic that returns the prefix for the full name of the proxy type to create.</param> /// <returns>The created type object.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="modBuilder" /> is <see langword="null" />. /// </exception> public Type CreateType(ModuleBuilder modBuilder, TypeNameProvider proxyTypeNamePrefixProvider) { return(this.CreateType(modBuilder, proxyTypeNamePrefixProvider, delegate(Type interfaceType) { return string.Format("_{0:N}_{1}", Guid.NewGuid(), this.GetHashCode()); })); }
/// <summary> /// Creates a propxy <see cref="Type" /> for the interface of <typeparamref name="T" />. /// </summary> /// <param name="modBuilder">The module builder to use.</param> /// <param name="proxyTypeNamePrefixProvider">The logic that returns the prefix for the full name of the proxy type to create.</param> /// <param name="proxyTypeNameSuffixProvider">The logic that returns the suffix for the full name of the proxy type to create.</param> /// <returns>The created type object.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="modBuilder" /> is <see langword="null" />. /// </exception> public Type CreateType(ModuleBuilder modBuilder, TypeNameProvider proxyTypeNamePrefixProvider, TypeNameProvider proxyTypeNameSuffixProvider) { return(this.CreateType(modBuilder, proxyTypeNamePrefixProvider, delegate(Type interfaceType) { return this.InterfaceType.Name; }, proxyTypeNameSuffixProvider)); }
protected override IList <JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) { var properties = base.CreateProperties(type, memberSerialization); if (type.IsException()) { var stackTrace = properties.Single(x => x.PropertyName == "StackTrace"); properties.Remove(stackTrace); properties.Add(stackTrace); properties.Insert(0, new() { PropertyName = "Type", PropertyType = typeof(string), ValueProvider = new TypeNameProvider(type), Ignored = false, Readable = true, Writable = false, }); } return(properties); }
/// <summary> /// Creates a propxy <see cref="Type" /> for the interface of <typeparamref name="T" />. /// </summary> /// <param name="modBuilder">The module builder to use.</param> /// <param name="proxyTypeNamePrefixProvider">The logic that returns the prefix for the full name of the proxy type to create.</param> /// <param name="proxyTypeNameProvider">The logic that returns the name part of the proxy type to create.</param> /// <param name="proxyTypeNameSuffixProvider">The logic that returns the suffix for the full name of the proxy type to create.</param> /// <returns>The created type object.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="modBuilder" /> is <see langword="null" />. /// </exception> public Type CreateType(ModuleBuilder modBuilder, TypeNameProvider proxyTypeNamePrefixProvider, TypeNameProvider proxyTypeNameProvider, TypeNameProvider proxyTypeNameSuffixProvider) { if (modBuilder == null) { throw new ArgumentNullException("modBuilder"); } string prefix = proxyTypeNamePrefixProvider == null ? null : (StringHelper.AsString(proxyTypeNamePrefixProvider(this.InterfaceType)) ?? string.Empty).Trim(); string name = proxyTypeNameProvider == null ? null : (StringHelper.AsString(proxyTypeNameProvider(this.InterfaceType)) ?? string.Empty).Trim(); string suffix = proxyTypeNameSuffixProvider == null ? null : (StringHelper.AsString(proxyTypeNameSuffixProvider(this.InterfaceType)) ?? string.Empty).Trim(); Type baseType = typeof(object); TypeBuilder typeBuilder = modBuilder.DefineType(string.Format("{0}{1}{2}", prefix, name, suffix), TypeAttributes.Public | TypeAttributes.Class, baseType); typeBuilder.AddInterfaceImplementation(this.InterfaceType); List <PropertyInfo> properties = new List <PropertyInfo>(); CollectProperties(properties, this.InterfaceType); // properties { foreach (PropertyInfo p in properties) { string propertyName = p.Name; Type propertyType = p.PropertyType; PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(propertyName, PropertyAttributes.None, propertyType, Type.EmptyTypes); string fieldName = char.ToLower(propertyName[0]) + new string(CollectionHelper.ToArray(CollectionHelper.Skip(propertyName, 1))); FieldBuilder field = typeBuilder.DefineField("_" + fieldName, propertyType, FieldAttributes.Family); // getter { MethodBuilder methodBuilder = typeBuilder.DefineMethod("get_" + propertyName, MethodAttributes.Public | MethodAttributes.Virtual, propertyType, Type.EmptyTypes); ILGenerator ilGen = methodBuilder.GetILGenerator(); ilGen.Emit(OpCodes.Ldarg_0); // load "this" ilGen.Emit(OpCodes.Ldfld, field); // load the property's underlying field onto the stack ilGen.Emit(OpCodes.Ret); // return the value on the stack propertyBuilder.SetGetMethod(methodBuilder); } // setter { MethodBuilder methodBuilder = typeBuilder.DefineMethod("set_" + propertyName, MethodAttributes.Public | MethodAttributes.Virtual, typeof(void), new Type[] { propertyType }); ILGenerator ilGen = methodBuilder.GetILGenerator(); ilGen.Emit(OpCodes.Ldarg_0); // load "this" ilGen.Emit(OpCodes.Ldarg_1); // load "value" onto the stack ilGen.Emit(OpCodes.Stfld, field); // set the field equal to the "value" on the stack ilGen.Emit(OpCodes.Ret); // return nothing propertyBuilder.SetSetMethod(methodBuilder); } } } // constructor { ConstructorInfo baseConstructor = baseType.GetConstructor(new Type[0]); ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes); ILGenerator ilGen = constructorBuilder.GetILGenerator(); ilGen.Emit(OpCodes.Ldarg_0); // load "this" ilGen.Emit(OpCodes.Call, baseConstructor); // call the base constructor //TODO // define initial values ilGen.Emit(OpCodes.Ret); // return nothing } return(typeBuilder.CreateType()); }