static void Main()
        {
            // Create a simple name for the assembly, and create the assembly and module.
            AssemblyName    myAssemblyName    = new AssemblyName("EmittedAssembly");
            AssemblyBuilder myAssemblyBuilder =
                AppDomain.CurrentDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
            ModuleBuilder myModuleBuilder =
                myAssemblyBuilder.DefineDynamicModule("EmittedAssembly", "EmittedAssembly.dll");

            // Define a public class named "MyDynamicClass" in the assembly.
            TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyDynamicClass",
                                                                   TypeAttributes.Public);

            // Create a permission set and add a security permission
            // with the ControlEvidence flag.
            //
            PermissionSet myPermissionSet = new PermissionSet(PermissionState.None);

            myPermissionSet.AddPermission(
                new SecurityPermission(SecurityPermissionFlag.ControlEvidence));

            // Add the permission set to the MyDynamicClass type,
            // as a declarative security demand.
            //
            myTypeBuilder.AddDeclarativeSecurity(SecurityAction.Demand, myPermissionSet);

            Type myType = myTypeBuilder.CreateType();

            myAssemblyBuilder.Save("EmittedAssembly.dll");
        }
示例#2
0
        public override void Emit()
        {
            base.Emit();

            if (declarative_security != null)
            {
                foreach (var de in declarative_security)
                {
#if STATIC
                    TypeBuilder.__AddDeclarativeSecurity(de);
#else
                    TypeBuilder.AddDeclarativeSecurity(de.Key, de.Value);
#endif
                }
            }

            var rtype = ReturnType.Type;
            if (rtype != null)
            {
                if (rtype.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
                {
                    Module.PredefinedAttributes.Dynamic.EmitAttribute(CreateReturnBuilder().Builder);
                }
                else if (rtype.HasDynamicElement)
                {
                    Module.PredefinedAttributes.Dynamic.EmitAttribute(CreateReturnBuilder().Builder, rtype, Location);
                }
                else if (rtype is ReadOnlyReferenceContainer)
                {
                    Module.PredefinedAttributes.IsReadOnly.EmitAttribute(CreateReturnBuilder().Builder);
                }

                if (rtype.HasNamedTupleElement)
                {
                    Module.PredefinedAttributes.TupleElementNames.EmitAttribute(CreateReturnBuilder().Builder, rtype, Location);
                }

                ConstraintChecker.Check(this, ReturnType.Type, ReturnType.Location);
            }

            Constructor.ParameterInfo.ApplyAttributes(this, Constructor.ConstructorBuilder);
            Constructor.ConstructorBuilder.SetImplementationFlags(MethodImplAttributes.Runtime);

            parameters.CheckConstraints(this);
            parameters.ApplyAttributes(this, InvokeBuilder.MethodBuilder);
            InvokeBuilder.MethodBuilder.SetImplementationFlags(MethodImplAttributes.Runtime);

            if (BeginInvokeBuilder != null)
            {
                BeginInvokeBuilder.ParameterInfo.ApplyAttributes(this, BeginInvokeBuilder.MethodBuilder);
                EndInvokeBuilder.ParameterInfo.ApplyAttributes(this, EndInvokeBuilder.MethodBuilder);

                BeginInvokeBuilder.MethodBuilder.SetImplementationFlags(MethodImplAttributes.Runtime);
                EndInvokeBuilder.MethodBuilder.SetImplementationFlags(MethodImplAttributes.Runtime);
            }
        }
示例#3
0
        public override void Emit()
        {
            base.Emit();

            if (declarative_security != null)
            {
                foreach (var de in declarative_security)
                {
#if STATIC
                    TypeBuilder.__AddDeclarativeSecurity(de);
#else
                    TypeBuilder.AddDeclarativeSecurity(de.Key, de.Value);
#endif
                }
            }

            if (ReturnType.Type != null)
            {
                if (ReturnType.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
                {
                    return_attributes = new ReturnParameter(this, InvokeBuilder.MethodBuilder, Location);
                    Module.PredefinedAttributes.Dynamic.EmitAttribute(return_attributes.Builder);
                }
                else if (ReturnType.Type.HasDynamicElement)
                {
                    return_attributes = new ReturnParameter(this, InvokeBuilder.MethodBuilder, Location);
                    Module.PredefinedAttributes.Dynamic.EmitAttribute(return_attributes.Builder, ReturnType.Type, Location);
                }

                ConstraintChecker.Check(this, ReturnType.Type, ReturnType.Location);
            }

            Constructor.ParameterInfo.ApplyAttributes(this, Constructor.ConstructorBuilder);
            Constructor.ConstructorBuilder.SetImplementationFlags(MethodImplAttributes.Runtime);

            parameters.CheckConstraints(this);
            parameters.ApplyAttributes(this, InvokeBuilder.MethodBuilder);
            InvokeBuilder.MethodBuilder.SetImplementationFlags(MethodImplAttributes.Runtime);

            if (BeginInvokeBuilder != null)
            {
                BeginInvokeBuilder.ParameterInfo.ApplyAttributes(this, BeginInvokeBuilder.MethodBuilder);
                EndInvokeBuilder.ParameterInfo.ApplyAttributes(this, EndInvokeBuilder.MethodBuilder);

                BeginInvokeBuilder.MethodBuilder.SetImplementationFlags(MethodImplAttributes.Runtime);
                EndInvokeBuilder.MethodBuilder.SetImplementationFlags(MethodImplAttributes.Runtime);
            }
        }
示例#4
0
				internal override void Apply(ClassLoaderWrapper loader, TypeBuilder tb, object annotation)
				{
					if (type == JVM.Import(typeof(System.Runtime.InteropServices.StructLayoutAttribute)) && tb.BaseType != Types.Object)
					{
						// we have to handle this explicitly, because if we apply an illegal StructLayoutAttribute,
						// TypeBuilder.CreateType() will later on throw an exception.
#if STATIC_COMPILER
						loader.IssueMessage(Message.IgnoredCustomAttribute, type.FullName, "Type '" + tb.FullName + "' does not extend cli.System.Object");
#else
						Tracer.Error(Tracer.Runtime, "StructLayoutAttribute cannot be applied to {0}, because it does not directly extend cli.System.Object", tb.FullName);
#endif
						return;
					}
					if (type.IsSubclassOf(Types.SecurityAttribute))
					{
#if STATIC_COMPILER
						tb.__AddDeclarativeSecurity(MakeCustomAttributeBuilder(loader, annotation));
#elif STUB_GENERATOR
#else
						SecurityAction action;
						PermissionSet permSet;
						if (MakeDeclSecurity(type, annotation, out action, out permSet))
						{
							tb.AddDeclarativeSecurity(action, permSet);
						}
#endif
					}
					else
					{
						tb.SetCustomAttribute(MakeCustomAttributeBuilder(loader, annotation));
					}
				}