Example #1
0
        public Type Build(AssemblyBuilderHelper assemblyBuilder)
        {
            if (assemblyBuilder == null)
            {
                throw new ArgumentNullException("assemblyBuilder");
            }

            // Check InternalsVisibleToAttributes of the source type's assembly.
            // Even if the sourceType is public, it may have internal fields and props.
            //
            _friendlyAssembly = false;

            // Usually, there is no such attribute in the source assembly.
            // Therefore we do not cache the result.
            //
            var attributes = _originalType.Type.Assembly.GetCustomAttributes(typeof(InternalsVisibleToAttribute), true);

            foreach (InternalsVisibleToAttribute visibleToAttribute in attributes)
            {
                var an = new AssemblyName(visibleToAttribute.AssemblyName);

                if (AssemblyName.ReferenceMatchesDefinition(assemblyBuilder.AssemblyName, an))
                {
                    _friendlyAssembly = true;
                    break;
                }
            }

            if (!_originalType.Type.IsVisible && !_friendlyAssembly)
            {
                return(typeof(ExprTypeAccessor <,>).MakeGenericType(_type, _originalType));
            }

            var typeName = GetTypeName();

            _typeBuilder = assemblyBuilder.DefineType(typeName, _accessorType);

            _typeBuilder.DefaultConstructor.Emitter
            .ldarg_0
            .call(TypeHelper.GetDefaultConstructor(_accessorType))
            ;

            BuildCreateInstanceMethods();
            BuildTypeProperties();
            BuildMembers();
            BuildObjectFactory();

            _typeBuilder.DefaultConstructor.Emitter
            .ret()
            ;

            var result = _typeBuilder.Create();

            foreach (TypeBuilderHelper tb in _nestedTypes)
            {
                tb.Create();
            }

            return(result);
        }
Example #2
0
        public Type Build(AssemblyBuilderHelper assemblyBuilder)
        {
            _typeBuilder = assemblyBuilder.DefineType(GetTypeName(), typeof(DuckType), _interfaceType);

            if (!BuildMembers(_interfaceType))
            {
                return(null);
            }

            foreach (Type t in _interfaceType.GetInterfaces())
            {
                if (!BuildMembers(t))
                {
                    return(null);
                }
            }

            return(_typeBuilder.Create());
        }
		public Type Build(AssemblyBuilderHelper assemblyBuilder)
		{
			if (assemblyBuilder == null) throw new ArgumentNullException("assemblyBuilder");

			// Check InternalsVisibleToAttributes of the source type's assembly.
			// Even if the sourceType is public, it may have internal fields and props.
			//
			_friendlyAssembly = false;

			// Usually, there is no such attribute in the source assembly.
			// Therefore we do not cache the result.
			//
			var attributes = _originalType.Type.Assembly.GetCustomAttributes(typeof(InternalsVisibleToAttribute), true);

			foreach (InternalsVisibleToAttribute visibleToAttribute in attributes)
			{
				var an = new AssemblyName(visibleToAttribute.AssemblyName);

				if (AssemblyName.ReferenceMatchesDefinition(assemblyBuilder.AssemblyName, an))
				{
					_friendlyAssembly = true;
					break;
				}
			}

			if (!_originalType.Type.IsVisible && !_friendlyAssembly || 
				(
					from p in _originalType.GetProperties(BindingFlags.Instance | BindingFlags.Public)
					from a in p.GetCustomAttributes(typeof(MapFieldAttribute), true).Cast<MapFieldAttribute>()
					where a.Storage != null
					select a
				).Any())
				return typeof (ExprTypeAccessor<,>).MakeGenericType(_type, _originalType);

			var typeName = GetTypeName();

			_typeBuilder = assemblyBuilder.DefineType(typeName, _accessorType);

			_typeBuilder.DefaultConstructor.Emitter
				.ldarg_0
				.call    (TypeHelper.GetDefaultConstructor(_accessorType))
				;

			BuildCreateInstanceMethods();
			BuildTypeProperties();
			BuildMembers();
			BuildObjectFactory();

			_typeBuilder.DefaultConstructor.Emitter
				.ret()
				;

			var result = _typeBuilder.Create();

			foreach (TypeBuilderHelper tb in _nestedTypes)
				tb.Create();

			return result;
		}
Example #4
0
        private static Type EnsureDelegateType(BuildContext context, MethodInfo method)
        {
            // The delegate should be defined as inner type of context.TypeBuilder.
            // It's possible, but we can not define and use newly defined type as Emit target in its owner type.
            // To solve this problem, we should create a top level delegate and make sure its name is unique.
            //
            string delegateName = context.TypeBuilder.TypeBuilder.FullName + "$" + method.Name + "$Delegate";
            Type   delegateType = (Type)context.Items[delegateName];

            if (delegateType == null)
            {
                ParameterInfo[] pi         = method.GetParameters();
                Type[]          parameters = new Type[pi.Length];

                for (int i = 0; i < pi.Length; i++)
                {
                    parameters[i] = pi[i].ParameterType;
                }

                const MethodImplAttributes mia = MethodImplAttributes.Runtime | MethodImplAttributes.Managed;
                const MethodAttributes     ma  = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual;

                TypeBuilderHelper delegateBuilder = context.AssemblyBuilder.DefineType(delegateName,
                                                                                       TypeAttributes.Class | TypeAttributes.NotPublic | TypeAttributes.Sealed | TypeAttributes.AnsiClass | TypeAttributes.AutoClass,
                                                                                       typeof(MulticastDelegate));

                // Create constructor
                //
                ConstructorBuilderHelper ctorBuilder = delegateBuilder.DefineConstructor(
                    MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.RTSpecialName, CallingConventions.Standard,
                    typeof(object), typeof(IntPtr));
                ctorBuilder.ConstructorBuilder.SetImplementationFlags(mia);

                MethodBuilderHelper methodBuilder;

                // Define the BeginInvoke method for the delegate
                //
                Type[] beginParameters = new Type[parameters.Length + 2];

                Array.Copy(parameters, 0, beginParameters, 0, parameters.Length);
                beginParameters[parameters.Length]     = typeof(AsyncCallback);
                beginParameters[parameters.Length + 1] = typeof(object);

                methodBuilder = delegateBuilder.DefineMethod("BeginInvoke", ma, typeof(IAsyncResult), beginParameters);
                methodBuilder.MethodBuilder.SetImplementationFlags(mia);

                // Define the EndInvoke method for the delegate
                //
                methodBuilder = delegateBuilder.DefineMethod("EndInvoke", ma, method.ReturnType, typeof(IAsyncResult));
                methodBuilder.MethodBuilder.SetImplementationFlags(mia);

                // Define the Invoke method for the delegate
                //
                methodBuilder = delegateBuilder.DefineMethod("Invoke", ma, method.ReturnType, parameters);
                methodBuilder.MethodBuilder.SetImplementationFlags(mia);

                context.Items[delegateName] = delegateType = delegateBuilder.Create();
            }

            return(delegateType);
        }
Example #5
0
        public Type Build(Type sourceType, AssemblyBuilderHelper assemblyBuilder)
        {
            if (sourceType == null)
            {
                throw new ArgumentNullException("sourceType");
            }
            if (assemblyBuilder == null)
            {
                throw new ArgumentNullException("assemblyBuilder");
            }

            // Check InternalsVisibleToAttributes of the source type's assembly.
            // Even if the sourceType is public, it may have internal fields and props.
            //
            _friendlyAssembly = false;

            // Usually, there is no such attribute in the source assembly.
            // Therefore we do not cache the result.
            //
            object[] attributes = sourceType.Assembly.GetCustomAttributes(typeof(InternalsVisibleToAttribute), true);

            foreach (InternalsVisibleToAttribute visibleToAttribute in attributes)
            {
                AssemblyName an = new AssemblyName(visibleToAttribute.AssemblyName);

                if (AssemblyName.ReferenceMatchesDefinition(assemblyBuilder.AssemblyName, an))
                {
                    _friendlyAssembly = true;
                    break;
                }
            }

            if (!sourceType.IsVisible && !_friendlyAssembly)
            {
                throw new TypeBuilderException(string.Format("Can not build type accessor for non-public type '{0}'.", sourceType.FullName));
            }

            string typeName = GetTypeAccessorClassName(_type);

            _typeBuilder = assemblyBuilder.DefineType(typeName, _accessorType);

            _typeBuilder.DefaultConstructor.Emitter
            .ldarg_0
            .call(TypeHelper.GetDefaultConstructor(_accessorType))
            ;

            BuildCreateInstanceMethods();
            BuildTypeProperties();
            BuildMembers();
            BuildObjectFactory();

            _typeBuilder.DefaultConstructor.Emitter
            .ret()
            ;

            Type result = _typeBuilder.Create();

            foreach (TypeBuilderHelper tb in _nestedTypes)
            {
                tb.Create();
            }

            return(result);
        }