Ejemplo n.º 1
0
        private static ConstructorInfo AddConstructor(TypeBuilder tb, MethodWrapper defaultConstructor, ConstructorInfo serializationConstructor, bool callReadObject)
        {
            ConstructorBuilder ctor = tb.DefineConstructor(MethodAttributes.Family, CallingConventions.Standard, new Type[] { JVM.Import(typeof(SerializationInfo)), JVM.Import(typeof(StreamingContext)) });

            AttributeHelper.HideFromJava(ctor);
            ctor.AddDeclarativeSecurity(SecurityAction.Demand, psetSerializationFormatter);
            CodeEmitter ilgen = CodeEmitter.Create(ctor);

            ilgen.Emit(OpCodes.Ldarg_0);
            if (defaultConstructor != null)
            {
                defaultConstructor.EmitCall(ilgen);
            }
            else
            {
                ilgen.Emit(OpCodes.Ldarg_1);
                ilgen.Emit(OpCodes.Ldarg_2);
                ilgen.Emit(OpCodes.Call, serializationConstructor);
            }
            if (callReadObject)
            {
                ilgen.Emit(OpCodes.Ldarg_0);
                ilgen.Emit(OpCodes.Ldarg_1);
                TypeWrapper   serializationHelper = ClassLoaderWrapper.LoadClassCritical("ikvm.internal.Serialization");
                MethodWrapper mw = serializationHelper.GetMethodWrapper("readObject", "(Ljava.lang.Object;Lcli.System.Runtime.Serialization.SerializationInfo;)V", false);
                mw.Link();
                mw.EmitCall(ilgen);
            }
            ilgen.Emit(OpCodes.Ret);
            ilgen.DoEmit();
            return(ctor);
        }
Ejemplo n.º 2
0
        public void AddDeclarativeSecurity_Action_Invalid()
        {
            ConstructorBuilder cb = genClass.DefineConstructor(
                MethodAttributes.Public, 0, new Type [0]);

            SecurityAction[] actions = new SecurityAction [] {
                SecurityAction.RequestMinimum,
                SecurityAction.RequestOptional,
                SecurityAction.RequestRefuse
            };
            PermissionSet set = new PermissionSet(PermissionState.Unrestricted);

            foreach (SecurityAction action in actions)
            {
                try {
                    cb.AddDeclarativeSecurity(action, set);
                    Assert.Fail("#1");
                } catch (ArgumentOutOfRangeException ex) {
                    Assert.AreEqual(typeof(ArgumentOutOfRangeException), ex.GetType(), "#2");
                    Assert.IsNull(ex.ActualValue, "#3");
                    Assert.IsNotNull(ex.Message, "#4");
                    Assert.AreEqual("action", ex.ParamName, "#5");
                }
            }
        }
Ejemplo n.º 3
0
        public void AddDeclarativeSecurity_Action_Duplicate()
        {
            ConstructorBuilder cb = genClass.DefineConstructor(
                MethodAttributes.Public, 0, new Type [0]);
            PermissionSet set = new PermissionSet(PermissionState.Unrestricted);

            cb.AddDeclarativeSecurity(SecurityAction.Demand, set);
            try {
                cb.AddDeclarativeSecurity(SecurityAction.Demand, set);
                Assert.Fail("#1");
            } catch (InvalidOperationException ex) {
                // Type has not been created
                Assert.AreEqual(typeof(InvalidOperationException), ex.GetType(), "#2");
                Assert.IsNull(ex.InnerException, "#3");
                Assert.IsNotNull(ex.Message, "#4");
            }
        }
Ejemplo n.º 4
0
        public void AddDeclarativeSecurity_PSet_Null()
        {
            ConstructorBuilder cb = genClass.DefineConstructor(
                MethodAttributes.Public, 0, new Type [0]);

            try {
                cb.AddDeclarativeSecurity(SecurityAction.Demand, null);
                Assert.Fail("#1");
            } catch (ArgumentNullException ex) {
                Assert.AreEqual(typeof(ArgumentNullException), ex.GetType(), "#2");
                Assert.IsNull(ex.InnerException, "#3");
                Assert.IsNotNull(ex.Message, "#4");
                Assert.AreEqual("pset", ex.ParamName, "#5");
            }
        }
Ejemplo n.º 5
0
				internal override void Apply(ClassLoaderWrapper loader, ConstructorBuilder cb, object annotation)
				{
					if (type.IsSubclassOf(Types.SecurityAttribute))
					{
#if STATIC_COMPILER
						cb.__AddDeclarativeSecurity(MakeCustomAttributeBuilder(loader, annotation));
#elif STUB_GENERATOR
#else
						SecurityAction action;
						PermissionSet permSet;
						if (MakeDeclSecurity(type, annotation, out action, out permSet))
						{
							cb.AddDeclarativeSecurity(action, permSet);
						}
#endif
					}
					else
					{
						cb.SetCustomAttribute(MakeCustomAttributeBuilder(loader, annotation));
					}
				}
Ejemplo n.º 6
0
    internal MyConstructorBuilder()
    {
// <Snippet1>
// <Snippet2>
// <Snippet3>
        MethodBuilder myMethodBuilder = null;

        AppDomain myCurrentDomain = AppDomain.CurrentDomain;
        // Create assembly in current CurrentDomain
        AssemblyName myAssemblyName = new AssemblyName();

        myAssemblyName.Name = "TempAssembly";
        // Create a dynamic assembly
        myAssemblyBuilder = myCurrentDomain.DefineDynamicAssembly
                                (myAssemblyName, AssemblyBuilderAccess.RunAndSave);
        // Create a dynamic module in the assembly.
        myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule");
        FieldInfo myFieldInfo =
            myModuleBuilder.DefineUninitializedData("myField", 2, FieldAttributes.Public);
        // Create a type in the module
        TypeBuilder  myTypeBuilder   = myModuleBuilder.DefineType("TempClass", TypeAttributes.Public);
        FieldBuilder myGreetingField = myTypeBuilder.DefineField("Greeting",
                                                                 typeof(String), FieldAttributes.Public);

        Type[] myConstructorArgs = { typeof(String) };
        // Define a constructor of the dynamic class.
        ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
            MethodAttributes.Public, CallingConventions.Standard, myConstructorArgs);
        PermissionSet myPset = new PermissionSet(PermissionState.Unrestricted);

        // Add declarative security to the constructor.
        Console.WriteLine("Adding declarative security to the constructor.....");
        Console.WriteLine("The Security action to be taken is \"DENY\" and" +
                          " Permission set is \"UNRESTRICTED\".");
        myConstructor.AddDeclarativeSecurity(SecurityAction.Deny, myPset);
// </Snippet3>
        MethodAttributes myMethodAttributes = myConstructor.Attributes;
        Type             myAttributeType    = typeof(MethodAttributes);
        int myAttribValue = (int)myMethodAttributes;

        if (!myAttributeType.IsEnum)
        {
            Console.WriteLine("This is not an Enum");
        }
        FieldInfo[] myFieldInfo1 = myAttributeType.GetFields(BindingFlags.Public | BindingFlags.Static);
        Console.WriteLine("The Field info names of the Attributes for the constructor are:");
        for (int i = 0; i < myFieldInfo1.Length; i++)
        {
            int myFieldValue = (Int32)myFieldInfo1[i].GetValue(null);
            if ((myFieldValue & myAttribValue) == myFieldValue)
            {
                Console.WriteLine("   " + myFieldInfo1[i].Name);
            }
        }

        Type myType2 = myConstructor.DeclaringType;

        Console.WriteLine("The declaring type is : " + myType2.ToString());
// </Snippet2>
        ParameterBuilder myParameterBuilder1 =
            myConstructor.DefineParameter(1, ParameterAttributes.Out, "My Parameter Name1");

        Console.WriteLine("The name of the parameter is : " +
                          myParameterBuilder1.Name);
        if (myParameterBuilder1.IsIn)
        {
            Console.WriteLine(myParameterBuilder1.Name + " is Input parameter.");
        }
        else
        {
            Console.WriteLine(myParameterBuilder1.Name + " is not Input Parameter.");
        }
        ParameterBuilder myParameterBuilder2 =
            myConstructor.DefineParameter(1, ParameterAttributes.In, "My Parameter Name2");

        Console.WriteLine("The Parameter name is : " +
                          myParameterBuilder2.Name);
        if (myParameterBuilder2.IsIn)
        {
            Console.WriteLine(myParameterBuilder2.Name + " is Input parameter.");
        }
        else
        {
            Console.WriteLine(myParameterBuilder2.Name + " is not Input Parameter.");
        }
// </Snippet1>
        // Generate MSIL for the method, call its base class constructor and store the arguments
        // in the private field.
        ILGenerator myILGenerator3 = myConstructor.GetILGenerator();

        myILGenerator3.Emit(OpCodes.Ldarg_0);
        ConstructorInfo myConstructorInfo = typeof(Object).GetConstructor(new Type[0]);

        myILGenerator3.Emit(OpCodes.Call, myConstructorInfo);
        myILGenerator3.Emit(OpCodes.Ldarg_0);
        myILGenerator3.Emit(OpCodes.Ldarg_1);
        myILGenerator3.Emit(OpCodes.Stfld, myGreetingField);
        myILGenerator3.Emit(OpCodes.Ret);
        // Add a method to the type.
        myMethodBuilder = myTypeBuilder.DefineMethod
                              ("HelloWorld", MethodAttributes.Public, null, null);
        // Generate MSIL for the method.
        ILGenerator myILGenerator2 = myMethodBuilder.GetILGenerator();

        myILGenerator2.EmitWriteLine("Hello World from global");
        myILGenerator2.Emit(OpCodes.Ret);
        myModuleBuilder.CreateGlobalFunctions();
        myType1 = myTypeBuilder.CreateType();
    }