Mono.Cecil.Fluent.DynamicTypeBuilder.Process C# (CSharp) Method

Process() public method

todo: properties, events, custom attributes, subtypes
public Process ( ) : Type
return System.Type
		public Type Process()
		{
			var parent = typeof(object);
			var interfaces = new List<Type>();
			var importer = TypeLoader.Instance;

			if (_type.BaseType != null)
			{
				parent = importer.Load(_type.BaseType);
				if (parent == null)
					throw new Exception($"can not resolve base type '{_type.BaseType.FullName}' in current app domain");
			}
			if (_type.Interfaces.Count != 0)
			{
				foreach (var @interface in _type.Interfaces)
				{
					var iface = importer.Load(@interface);
					if (iface == null)
						throw new Exception($"can not resolve interface type '{@interface.FullName}' in current app domain");
                    interfaces.Add(iface);
				}
			}

			var typebuilder = CreateTypeBuilder(_type.Name, (System.Reflection.TypeAttributes) _type.Attributes, parent, interfaces);

			foreach (var field in _type.Fields)
			{
				var fieldtype = importer.Load(field.FieldType);
				if (fieldtype == null)
					throw new Exception($"can not resolve type '{field.FieldType.FullName}' for field '{field.Name}' in current app domain");
                typebuilder.DefineField(field.Name, fieldtype, (System.Reflection.FieldAttributes) field.Attributes);
			}

			foreach (var method in _type.Methods)
			{
				var returntype = importer.Load(method.ReturnType);
				if (returntype == null)
					throw new Exception($"can not resolve type '{method.ReturnType.FullName}' for method '{method.Name}' in current app domain");
                var paramtypes = new List<Type>();
				foreach (var param in method.Parameters)
				{
					var t = importer.Load(param.ParameterType);
					if (t == null)
						throw new Exception($"can not resolve type '{method.ReturnType.FullName}' for method '{method.Name}' parameter '{param.Name}' in current app domain");
                    paramtypes.Add(t);
				}
				var mb = typebuilder.DefineMethod(method.Name, (System.Reflection.MethodAttributes) method.Attributes, returntype, paramtypes.ToArray());
				method.ToDynamicMethod(typebuilder, mb);
			}

			return typebuilder.CreateType();
		}