using System;
using System.Reflection;
using System.Collections;
+using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Diagnostics.SymbolStore;
using System.Globalization;
namespace System.Reflection.Emit {
-#if NET_2_0
[ComVisible (true)]
[ComDefaultInterface (typeof (_ModuleBuilder))]
-#endif
[ClassInterface (ClassInterfaceType.None)]
public class ModuleBuilder : Module, _ModuleBuilder {
private TypeBuilder global_type;
private Type global_type_created;
Hashtable name_cache;
- Hashtable us_string_cache = new Hashtable ();
+ Dictionary<string, int> us_string_cache;
private int[] table_indexes;
bool transient;
ModuleBuilderTokenGenerator token_gen;
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern void basic_init (ModuleBuilder ab);
+ [MethodImplAttribute(MethodImplOptions.InternalCall)]
+ private static extern void set_wrappers_type (ModuleBuilder mb, Type ab);
+
internal ModuleBuilder (AssemblyBuilder assb, string name, string fullyqname, bool emitSymbolInfo, bool transient) {
this.name = this.scopename = name;
this.fqname = fullyqname;
// guid = Guid.NewGuid().ToByteArray ();
table_idx = get_next_table_index (this, 0x00, true);
name_cache = new Hashtable ();
+ us_string_cache = new Dictionary<string, int> (512);
basic_init (this);
CreateGlobalType ();
-
+
+ if (assb.IsRun) {
+ TypeBuilder tb = new TypeBuilder (this, TypeAttributes.Abstract, 0xFFFFFF); /*last valid token*/
+ Type type = tb.CreateType ();
+ set_wrappers_type (this, type);
+ }
+
if (emitSymbolInfo) {
+#if MOONLIGHT
+ symbolWriter = new Mono.CompilerServices.SymbolWriter.SymbolWriterImpl (this);
+#else
Assembly asm = Assembly.LoadWithPartialName ("Mono.CompilerServices.SymbolWriter");
if (asm == null)
throw new ExecutionEngineException ("The assembly for default symbol writer cannot be loaded");
throw new ExecutionEngineException ("The type that implements the default symbol writer interface cannot be found");
symbolWriter = (ISymbolWriter) Activator.CreateInstance (t, new object[] { this });
+#endif
string fileName = fqname;
if (assemblyb.AssemblyDir != null)
fileName = Path.Combine (assemblyb.AssemblyDir, fileName);
if ((size <= 0) || (size > 0x3f0000))
throw new ArgumentException ("size", "Data size must be > 0 and < 0x3f0000");
- if (global_type == null)
- global_type = new TypeBuilder (this, 0);
+ CreateGlobalType ();
string typeName = "$ArrayType$" + size;
Type datablobtype = GetType (typeName, false, false);
return DefineGlobalMethod (name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
}
-#if NET_2_0 || BOOTSTRAP_NET_2_0
- public
-#else
- internal
-#endif
- MethodBuilder DefineGlobalMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers)
+ public MethodBuilder DefineGlobalMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers)
{
if (name == null)
throw new ArgumentNullException ("name");
throw new ArgumentException ("global methods must be static");
if (global_type_created != null)
throw new InvalidOperationException ("global methods already created");
- if (global_type == null)
- global_type = new TypeBuilder (this, 0);
+ CreateGlobalType ();
MethodBuilder mb = global_type.DefineMethod (name, attributes, callingConvention, returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers, parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers);
addGlobalMethod (mb);
throw new ArgumentException ("global methods must be static");
if (global_type_created != null)
throw new InvalidOperationException ("global methods already created");
- if (global_type == null)
- global_type = new TypeBuilder (this, 0);
+ CreateGlobalType ();
MethodBuilder mb = global_type.DefinePInvokeMethod (name, dllName, entryName, attributes, callingConvention, returnType, parameterTypes, nativeCallConv, nativeCharSet);
addGlobalMethod (mb);
}
private TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packingSize, int typesize) {
+ if (name == null)
+ throw new ArgumentNullException ("fullname");
+ if (name_cache.ContainsKey (name))
+ throw new ArgumentException ("Duplicate type name within an assembly.");
TypeBuilder res = new TypeBuilder (this, name, attr, parent, interfaces, packingSize, typesize, null);
AddType (res);
-
- try {
- name_cache.Add (name, res);
- } catch {
- throw new ArgumentException ("Duplicate type name within an assembly.");
- }
+
+ name_cache.Add (name, res);
return res;
}
return (TypeBuilder) name_cache [name];
}
-#if NET_2_0
[ComVisible (true)]
-#endif
public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
return DefineType (name, attr, parent, interfaces, PackingSize.Unspecified, TypeBuilder.UnspecifiedTypeSize);
}
return eb;
}
-#if NET_2_0
[ComVisible (true)]
-#endif
public override Type GetType( string className) {
return GetType (className, false, false);
}
-#if NET_2_0
[ComVisible (true)]
-#endif
public override Type GetType( string className, bool ignoreCase) {
return GetType (className, false, ignoreCase);
}
return null;
}
-#if NET_2_0
[ComVisible (true)]
-#endif
- public override Type GetType (string className, bool throwOnError, bool ignoreCase) {
+ public override Type GetType (string className, bool throwOnError, bool ignoreCase)
+ {
+ if (className == null)
+ throw new ArgumentNullException ("className");
+ if (className.Length == 0)
+ throw new ArgumentException ("className");
+
int subt;
string orig = className;
string modifiers;
}
}
-#if NET_2_0
[ComVisible (true)]
-#endif
public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
}
throw new NotImplementedException ();
}
-#if NET_2_0
public void DefineManifestResource (string name, Stream stream, ResourceAttributes attribute) {
if (name == null)
throw new ArgumentNullException ("name");
resources [p].attrs = attribute;
resources [p].stream = stream;
}
-#endif
[MonoTODO]
public void SetSymCustomAttribute (string name, byte[] data)
return GetMethodToken (GetArrayMethod (arrayClass, methodName, callingConvention, returnType, parameterTypes));
}
-#if NET_2_0
[ComVisible (true)]
-#endif
public MethodToken GetConstructorToken (ConstructorInfo con)
{
if (con == null)
private static extern int getMethodToken (ModuleBuilder mb, MethodInfo method,
Type[] opt_param_types);
- internal int GetToken (string str) {
- if (us_string_cache.Contains (str))
- return (int)us_string_cache [str];
- int result = getUSIndex (this, str);
- us_string_cache [str] = result;
+ internal int GetToken (string str)
+ {
+ int result;
+ if (!us_string_cache.TryGetValue (str, out result)) {
+ result = getUSIndex (this, str);
+ us_string_cache [str] = result;
+ }
+
return result;
}
internal void CreateGlobalType () {
if (global_type == null)
- global_type = new TypeBuilder (this, 0);
+ global_type = new TypeBuilder (this, 0, 1);
}
internal override Guid GetModuleVersionId ()
{
throw new NotImplementedException ();
}
+
+#if NET_4_0
+ public override Assembly Assembly {
+ get { return assemblyb; }
+ }
+
+ public override string Name {
+ get { return name; }
+ }
+
+ public override string ScopeName {
+ get { return name; }
+ }
+
+ public override Guid ModuleVersionId {
+ get {
+ return GetModuleVersionId ();
+ }
+ }
+
+ //XXX resource modules can't be defined with ModuleBuilder
+ public override bool IsResource ()
+ {
+ return false;
+ }
+
+ protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
+ {
+ if (global_type_created == null)
+ return null;
+ if (types == null)
+ return global_type_created.GetMethod (name);
+ return global_type_created.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
+ }
+
+ public override FieldInfo ResolveField (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
+ ResolveTokenError error;
+
+ IntPtr handle = ResolveFieldToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
+ if (handle == IntPtr.Zero)
+ throw resolve_token_exception (metadataToken, error, "Field");
+ else
+ return FieldInfo.GetFieldFromHandle (new RuntimeFieldHandle (handle));
+ }
+
+ public override MemberInfo ResolveMember (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
+
+ ResolveTokenError error;
+
+ MemberInfo m = ResolveMemberToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
+ if (m == null)
+ throw resolve_token_exception (metadataToken, error, "MemberInfo");
+ else
+ return m;
+ }
+
+ public override MethodBase ResolveMethod (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
+ ResolveTokenError error;
+
+ IntPtr handle = ResolveMethodToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
+ if (handle == IntPtr.Zero)
+ throw resolve_token_exception (metadataToken, error, "MethodBase");
+ else
+ return MethodBase.GetMethodFromHandleNoGenericCheck (new RuntimeMethodHandle (handle));
+ }
+
+ public override string ResolveString (int metadataToken) {
+ ResolveTokenError error;
+
+ string s = ResolveStringToken (_impl, metadataToken, out error);
+ if (s == null)
+ throw resolve_token_exception (metadataToken, error, "string");
+ else
+ return s;
+ }
+
+ public override byte[] ResolveSignature (int metadataToken) {
+ ResolveTokenError error;
+
+ byte[] res = ResolveSignature (_impl, metadataToken, out error);
+ if (res == null)
+ throw resolve_token_exception (metadataToken, error, "signature");
+ else
+ return res;
+ }
+
+ public override Type ResolveType (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
+ ResolveTokenError error;
+
+ IntPtr handle = ResolveTypeToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
+ if (handle == IntPtr.Zero)
+ throw resolve_token_exception (metadataToken, error, "Type");
+ else
+ return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
+ }
+
+#endif
}
internal class ModuleBuilderTokenGenerator : TokenGenerator {