本文整理汇总了C#中System.Reflection.Emit.ModuleBuilder.DefineType方法的典型用法代码示例。如果您正苦于以下问题:C# ModuleBuilder.DefineType方法的具体用法?C# ModuleBuilder.DefineType怎么用?C# ModuleBuilder.DefineType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.ModuleBuilder
的用法示例。
在下文中一共展示了ModuleBuilder.DefineType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EmitOwnerMappingType
private TypeBuilder EmitOwnerMappingType(IPropertyMappingProvider map, ModuleBuilder defineDynamicModule, string ownerTypeName)
{
var owner = defineDynamicModule.DefineType(
ownerTypeName,
TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract,
null,
new[] { typeof(IRdfListOwner) }).CreateType();
var ownerMapType = typeof(ListOwnerMap<>).MakeGenericType(new[] { owner });
var mapBuilderHelper = defineDynamicModule.DefineType(ownerTypeName + "Map", TypeAttributes.Public, ownerMapType);
var propertyBuilder = mapBuilderHelper.DefineProperty("ListPredicate", PropertyAttributes.None, typeof(Uri), null);
var getMethod = mapBuilderHelper.DefineMethod(
"get_ListPredicate",
MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Virtual | MethodAttributes.HideBySig | MethodAttributes.SpecialName,
typeof(Uri),
Type.EmptyTypes);
propertyBuilder.SetGetMethod(getMethod);
var ilGenerator = getMethod.GetILGenerator();
ilGenerator.Emit(OpCodes.Nop);
ilGenerator.Emit(OpCodes.Ldstr, map.GetTerm(_ontologyProvider).ToString());
ilGenerator.Emit(OpCodes.Newobj, typeof(Uri).GetConstructor(new[] { typeof(string) }));
ilGenerator.Emit(OpCodes.Ret);
return mapBuilderHelper;
}
示例2: DotNetCompiler
public DotNetCompiler(LibraryContext context, string filename)
{
Context = context;
var fi = new FileInfo(filename);
Path = fi;
BuilderAppDomain = AppDomain.CurrentDomain;
AssemblyBuilder = BuilderAppDomain.DefineDynamicAssembly(new AssemblyName(fi.Name), AssemblyBuilderAccess.RunAndSave, fi.DirectoryName);
MainModule = AssemblyBuilder.DefineDynamicModule(fi.Name, fi.Name);
ScriptsType = MainModule.DefineType("Scripts", TypeAttributes.Class);
RoomsType = MainModule.DefineType("Rooms", TypeAttributes.Class);
}
示例3: CreateWrappers
private static void CreateWrappers(Type interfaceType, ModuleBuilder mb,
out TypeBuilder rawInterface, out TypeBuilder CCP, out TypeBuilder RCP)
{
idCounter += 1;
List<MethodDesc> methodDescs = new List<MethodDesc>();
foreach (MethodInfo mi in interfaceType.GetMethods())
{
methodDescs.Add(new MethodDesc(mi));
}
rawInterface = mb.DefineType(interfaceType.FullName + "-RAW" + idCounter, TypeAttributes.Interface);
CCW = mb.DefineType(interfaceType.FullName + "-CCP" + idCounter, TypeAttributes.Class);
RCW = mb.DefineType(interfaceType.FullName + "-RCP" + idCounter, TypeAttributes.Class);
}
示例4: GenerateAnonymousType
private Type GenerateAnonymousType(ModuleBuilder dynamicTypeModule)
{
var dynamicType = dynamicTypeModule.DefineType(ClassName, TypeAttributes.BeforeFieldInit | TypeAttributes.Sealed);
var builderArray = dynamicType.DefineGenericParameters(GenericClassParameterNames);
var fields = new List<FieldBuilder>();
for (var i = 0; i < FieldNames.Length; i++)
{
var item = dynamicType.DefineField(FieldNames[i], builderArray[i], FieldAttributes.InitOnly | FieldAttributes.Private);
var type = typeof(DebuggerBrowsableAttribute);
var customBuilder = new CustomAttributeBuilder(type.GetConstructor(new Type[] { typeof(DebuggerBrowsableState) }), new object[] { DebuggerBrowsableState.Never });
item.SetCustomAttribute(customBuilder);
fields.Add(item);
}
var list2 = new List<PropertyBuilder>();
for (var j = 0; j < PropertyNames.Length; j++)
{
var builder4 = GenerateProperty(dynamicType, PropertyNames[j], fields[j]);
// var type = typeof(JsonPropertyAttribute);
// var customBuilder2 = new CustomAttributeBuilder(type.GetConstructor(new Type[0]), new object[0]);
// builder4.SetCustomAttribute(customBuilder2);
list2.Add(builder4);
}
GenerateClassAttributes(dynamicType, PropertyNames);
GenerateConstructor(dynamicType, PropertyNames, fields);
GenerateEqualsMethod(dynamicType, fields.ToArray());
GenerateGetHashCodeMethod(dynamicType, fields.ToArray());
GenerateToStringMethod(dynamicType, PropertyNames, fields.ToArray());
return dynamicType.CreateType().MakeGenericType(PropertyTypes);
}
示例5: DefineParallelizedType
public Type DefineParallelizedType(ModuleBuilder moduleBuilder)
{
TypeBuilder typeBuilder = moduleBuilder.DefineType(ParallelizedTypeName);
typeBuilder.SetParent(source);
TypeAnalyzer typeAnalyzer = new TypeAnalyzer(source);
ChannelImplementer channelImplementer = new ChannelImplementer(typeBuilder);
ForkImplementer forkImplementer = new ForkImplementer(typeBuilder, channelImplementer);
ChordImplementer chordImplementer = new ChordImplementer(typeBuilder, channelImplementer);
JoinImplementer joinImplementer = new JoinImplementer(typeBuilder, channelImplementer);
YieldImplementer yieldImplementer = new YieldImplementer(typeBuilder, channelImplementer);
DisposeImplementer disposeImplementer = new DisposeImplementer(typeBuilder, source);
ConstructorImplementer constructorImplementer = new ConstructorImplementer(typeBuilder, source,
channelImplementer,
chordImplementer,
disposeImplementer);
foreach (ForkGroup forkGroup in typeAnalyzer.GetForkGroups())
forkImplementer.Implement(forkGroup);
foreach (ChordInfo chord in typeAnalyzer.GetChords())
chordImplementer.Implement(chord);
foreach (JoinGroup joinGroup in typeAnalyzer.GetJoinGroups())
joinImplementer.Implement(joinGroup);
foreach (YieldInfo yieldInfo in typeAnalyzer.GetYields())
yieldImplementer.Implement(yieldInfo);
disposeImplementer.ImplementDisposalBehavior();
constructorImplementer.ImplementConstructor();
return typeBuilder.CreateType();
}
示例6: SerializerEmitter
/// <summary>
/// Initializes a new instance of the <see cref="SerializerEmitter"/> class.
/// </summary>
/// <param name="host">The host <see cref="ModuleBuilder"/>.</param>
/// <param name="specification">The specification of the serializer.</param>
/// <param name="baseClass">Type of the base class of the serializer.</param>
/// <param name="isDebuggable">Set to <c>true</c> when <paramref name="host"/> is debuggable.</param>
public SerializerEmitter( ModuleBuilder host, SerializerSpecification specification, Type baseClass, bool isDebuggable )
{
Contract.Requires( host != null );
Contract.Requires( specification != null );
Contract.Requires( baseClass != null );
Tracer.Emit.TraceEvent( Tracer.EventType.DefineType, Tracer.EventId.DefineType, "Create {0}", specification.SerializerTypeFullName );
this._methodTable = new Dictionary<string, MethodBuilder>();
this._fieldTable = new Dictionary<string, FieldBuilder>();
this._specification = specification;
this._host = host;
this._typeBuilder =
host.DefineType(
specification.SerializerTypeFullName,
TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.UnicodeClass | TypeAttributes.AutoLayout | TypeAttributes.BeforeFieldInit,
baseClass
);
#if DEBUG
Contract.Assert( this._typeBuilder.BaseType != null, "baseType != null" );
#endif // DEBUG
this._isDebuggable = isDebuggable;
#if DEBUG && !NETFX_35 && !NETSTANDARD1_1 && !NETSTANDARD1_3
if ( isDebuggable && SerializerDebugging.DumpEnabled )
{
SerializerDebugging.PrepareDump( host.Assembly as AssemblyBuilder );
}
#endif // DEBUG && !NETFX_35 && !NETSTANDARD1_1 && !NETSTANDARD1_3
}
示例7: EmitType
void EmitType(ModuleBuilder Parent, CodeTypeDeclaration Decl)
{
TypeBuilder Type = Parent.DefineType(Decl.Name, TypeAttributes.Public);
// Allow for late binding
var LocalMethods = new Dictionary<string, MethodWriter>();
var LocalParameters = new Dictionary<string, Type[]>();
foreach(CodeMemberMethod Method in Decl.Members)
{
var Writer = new MethodWriter(Type, Method, Methods, Mirror);
LocalParameters.Add(Method.Name, GetParameterTypes(Method.Parameters));
LocalMethods.Add(Method.Name, Writer);
}
foreach(var Writer in LocalMethods.Values)
{
Writer.ParameterTypes = LocalParameters;
Writer.Methods = LocalMethods;
Writer.Emit();
if(Writer.IsEntryPoint) EntryPoint = Writer.Method;
}
Type.CreateType();
}
示例8: DefineDelegateType
/// <summary>
/// Defines a new delegate type.
/// </summary>
public static System.Type DefineDelegateType(
string fullTypeName,
ModuleBuilder moduleBuilder,
System.Type returnType,
System.Type[] parameterTypes)
{
TypeBuilder delegateBuilder =
moduleBuilder.DefineType(
fullTypeName,
TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.AnsiClass |
TypeAttributes.AutoClass, typeof(MulticastDelegate));
// Define a special constructor
ConstructorBuilder constructorBuilder =
delegateBuilder.DefineConstructor(
MethodAttributes.RTSpecialName | MethodAttributes.HideBySig | MethodAttributes.Public,
CallingConventions.Standard, new System.Type[] {typeof(object), typeof(IntPtr)});
constructorBuilder.SetImplementationFlags(
MethodImplAttributes.Runtime | MethodImplAttributes.Managed);
// Define the Invoke method for the delegate
MethodBuilder methodBuilder;
methodBuilder =
delegateBuilder.DefineMethod(
"Invoke",
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot |
MethodAttributes.Virtual, returnType, parameterTypes);
methodBuilder.SetImplementationFlags(
MethodImplAttributes.Runtime | MethodImplAttributes.Managed);
return delegateBuilder.CreateType();
}
示例9: BuildRedisType
private static Type BuildRedisType(ModuleBuilder moduleBuilder, Type redisPipelineType)
{
const TypeAttributes typeAttributes = TypeAttributes.Public | TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit;
const MethodAttributes constructorAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
var typeBuilder = moduleBuilder.DefineType("Redis.Dynamic.RedisDynamic", typeAttributes, typeof(RedisBase), Type.EmptyTypes);
// Emit constructor:
// public RedisDynamic(IRedisConnection redisConnection)
var constructorBuilder = typeBuilder.DefineConstructor(constructorAttributes, CallingConventions.Standard, new[] { typeof(IRedisConnection) });
var redisConstructor = typeof(RedisBase).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(IRedisConnection) }, null);
var generator = constructorBuilder.GetILGenerator();
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldarg_1);
generator.Emit(OpCodes.Call, redisConstructor);
generator.Emit(OpCodes.Ret);
// Emit all abstract RedisBase methods
var redisCommandMethods = typeof(RedisBase).GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var redisCommandMethod in redisCommandMethods.Where(x => x.Name != "CreatePipeline" && x.IsAbstract))
{
EmitRedisCommandMethod(typeBuilder, redisCommandMethod);
}
// Emit CreatePipeline method
EmitRedisPipelineMethod(typeBuilder, redisPipelineType);
return typeBuilder.CreateType();
}
示例10: CreateTypeFromInterface
static Type CreateTypeFromInterface(ModuleBuilder builder, Type typeToProxy)
{
string typeName = typeToProxy.Namespace + ProxyNamespaceSuffix + "." + typeToProxy.Name;
TypeBuilder typeBuilder = builder.DefineType(typeName, TypeAttributes.Serializable | TypeAttributes.Class |
TypeAttributes.Public | TypeAttributes.Sealed,
typeof(object), new[] {typeToProxy});
typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
typeToProxy.GetAllProperties().Each(x =>
{
FieldBuilder fieldBuilder = typeBuilder.DefineField("field_" + x.Name, x.PropertyType, FieldAttributes.Private);
PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(x.Name, x.Attributes | PropertyAttributes.HasDefault,
x.PropertyType, null);
MethodBuilder getMethod = GetGetMethodBuilder(x, typeBuilder, fieldBuilder);
MethodBuilder setMethod = GetSetMethodBuilder(x, typeBuilder, fieldBuilder);
propertyBuilder.SetGetMethod(getMethod);
propertyBuilder.SetSetMethod(setMethod);
});
return typeBuilder.CreateType();
}
示例11: ILDynamicMethodDebugImpl
public ILDynamicMethodDebugImpl(string name, Type delegateType, Type thisType)
{
_delegateType = delegateType;
_expectedLength = 64;
var mi = delegateType.GetMethod("Invoke");
var uniqueName = ILDynamicTypeDebugImpl.UniqueName(name);
_assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(uniqueName), AssemblyBuilderAccess.RunAndSave, DynamicILDirectoryPath.DynamicIL);
_moduleBuilder = _assemblyBuilder.DefineDynamicModule(uniqueName + ".dll", true);
var sourceCodeFileName = Path.Combine(DynamicILDirectoryPath.DynamicIL, uniqueName + ".il");
_symbolDocumentWriter = _moduleBuilder.DefineDocument(sourceCodeFileName, SymDocumentType.Text, SymLanguageType.ILAssembly, SymLanguageVendor.Microsoft);
_sourceCodeWriter = new SourceCodeWriter(sourceCodeFileName, _symbolDocumentWriter);
Type[] parameterTypes;
if (thisType != null)
{
parameterTypes = new[] { thisType }.Concat(mi.GetParameters().Select(pi => pi.ParameterType)).ToArray();
}
else
{
parameterTypes = mi.GetParameters().Select(pi => pi.ParameterType).ToArray();
}
_sourceCodeWriter.StartMethod(name, mi.ReturnType, parameterTypes, MethodAttributes.Static);
_typeBuilder = _moduleBuilder.DefineType(name, TypeAttributes.Public, typeof(object), Type.EmptyTypes);
_forbidenInstructions = new ILGenForbidenInstructionsCheating(_typeBuilder);
_dynamicMethod = _typeBuilder.DefineMethod("Invoke", MethodAttributes.Public | MethodAttributes.Static, mi.ReturnType, parameterTypes);
for (int i = 0; i < parameterTypes.Length; i++)
{
_dynamicMethod.DefineParameter(i + 1, ParameterAttributes.In, string.Format("arg{0}", i));
}
}
示例12: Build
internal static TypeBuilder Build(ModuleBuilder proxyModule, Type proxyType,
AssemblyDebugging debugAssembly)
{
TypeBuilder proxyTypeBuilder = null;
string proxyName = proxyType.Namespace + "." +
proxyType.Name + Proxy.ProxyExtension;
var iProxyType = typeof(IProxy<>).MakeGenericType(proxyType);
proxyTypeBuilder = proxyModule.DefineType(proxyName,
TypeAttributes.Class | TypeAttributes.Sealed |
TypeAttributes.Public, proxyType, new Type[] { iProxyType });
using (TypeDebugging debugType = debugAssembly.GetTypeDebugging(proxyTypeBuilder))
{
var fields = ProxyFieldBuilder.Build(
proxyTypeBuilder, proxyType);
ProxyConstructorBuilder.Build(proxyTypeBuilder, proxyType,
fields[ProxyFieldBuilder.WrappedObjectField],
fields[ProxyFieldBuilder.InvokeHandlerField], debugType);
ProxyMethodBuilder.Build(proxyTypeBuilder, proxyType, iProxyType,
fields[ProxyFieldBuilder.WrappedObjectField],
fields[ProxyFieldBuilder.InvokeHandlerField], debugType);
}
return proxyTypeBuilder;
}
示例13: GetTypeBuilder
private TypeBuilder GetTypeBuilder(Type sourceType, ModuleBuilder moduleBuilder)
{
return moduleBuilder.DefineType(
SnapshotNameGenerator.Generate(sourceType),
TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed,
SnapshotBaseType);
}
示例14: AbstractProxyBuilder
public AbstractProxyBuilder(ModuleBuilder moduleBuilder, string className, Type interfaceType, Type innerType)
{
Verify.ArgumentNotNull(moduleBuilder, "moduleBuilder");
Verify.ArgumentNotNull(className, "className");
Verify.ArgumentNotNull(interfaceType, "interfaceType");
Verify.ArgumentNotNull(innerType, "innerType");
if (!interfaceType.IsInterface) {
throw new ArgumentException("must be an interface type", "interfaceType");
}
_errorMessages = new List<string>();
_moduleBuilder = moduleBuilder;
_className = className;
_interfaceType = interfaceType;
_innerType = innerType;
_typeBuilder = _moduleBuilder.DefineType(
_className,
TypeAttributes.Public |
TypeAttributes.Class |
TypeAttributes.AutoClass |
TypeAttributes.AnsiClass |
TypeAttributes.BeforeFieldInit |
TypeAttributes.AutoLayout,
typeof(object),
new Type[] {_interfaceType});
_innerFieldBuilder = _typeBuilder.DefineField("inner", _innerType, FieldAttributes.Private);
}
示例15: Program
public void Program()
{
var tempName = "EvilProg-" + Guid.NewGuid().ToString();
AssemblyName aName = new AssemblyName(tempName);
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Save);
programModule = ab.DefineDynamicModule(aName.Name, tempName + ".exe", true);
programType = programModule.DefineType("Program", TypeAttributes.Public);
createReadInInt();
this.program();
var main = functionMap["main"];
programModule.SetUserEntryPoint(main);
ab.SetEntryPoint(main);
programType.CreateType();
string tempPath = tempName + ".exe";
ab.Save(tempPath);
if (File.Exists(Options.ClrExec.Value))
File.Delete(Options.ClrExec.Value);
File.Move(tempPath, Options.ClrExec.Value);
File.Delete(tempPath);
}