예제 #1
0
        public static void TestPseudoCustomAttributes()
        {
            MethodInfo m = typeof(ParametersWithPseudoCustomtAttributes).Project().GetTypeInfo().GetDeclaredMethod("Foo");

            ParameterInfo[] pis = m.GetParameters();
            {
                ParameterInfo       p   = pis[0];
                CustomAttributeData cad = p.CustomAttributes.Single(c => c.AttributeType == typeof(InAttribute).Project());
                InAttribute         i   = cad.UnprojectAndInstantiate <InAttribute>();
            }

            {
                ParameterInfo       p   = pis[1];
                CustomAttributeData cad = p.CustomAttributes.Single(c => c.AttributeType == typeof(OutAttribute).Project());
                OutAttribute        o   = cad.UnprojectAndInstantiate <OutAttribute>();
            }

            {
                ParameterInfo       p   = pis[2];
                CustomAttributeData cad = p.CustomAttributes.Single(c => c.AttributeType == typeof(OptionalAttribute).Project());
                OptionalAttribute   o   = cad.UnprojectAndInstantiate <OptionalAttribute>();
            }

            {
                ParameterInfo       p   = pis[3];
                CustomAttributeData cad = p.CustomAttributes.Single(c => c.AttributeType == typeof(MarshalAsAttribute).Project());
                MarshalAsAttribute  ma  = cad.UnprojectAndInstantiate <MarshalAsAttribute>();
                Assert.Equal(UnmanagedType.I4, ma.Value);
            }
        }
예제 #2
0
        public static void TestMarshalAsPseudoCustomAttribute(string fieldName, MarshalAsAttribute expected)
        {
            TypeInfo  ecmaType  = typeof(MarshalAsHolders).Project().GetTypeInfo();
            FieldInfo ecmaField = ecmaType.GetDeclaredField(fieldName);

            Assert.NotNull(ecmaField);
            CustomAttributeData cad    = ecmaField.CustomAttributes.Single(c => c.AttributeType.Name == nameof(MarshalAsAttribute));
            MarshalAsAttribute  actual = cad.UnprojectAndInstantiate <MarshalAsAttribute>();

            AssertEqual(expected, actual);
        }
예제 #3
0
        public static void TestExplicitOffsetPseudoCustomAttribute()
        {
            Type t = typeof(ExplicitFieldOffsets).Project();

            {
                FieldInfo            f   = t.GetField("X");
                CustomAttributeData  cad = f.CustomAttributes.Single(c => c.AttributeType == typeof(FieldOffsetAttribute).Project());
                FieldOffsetAttribute foa = cad.UnprojectAndInstantiate <FieldOffsetAttribute>();
                Assert.Equal(42, foa.Value);
            }

            {
                FieldInfo            f   = t.GetField("Y");
                CustomAttributeData  cad = f.CustomAttributes.Single(c => c.AttributeType == typeof(FieldOffsetAttribute).Project());
                FieldOffsetAttribute foa = cad.UnprojectAndInstantiate <FieldOffsetAttribute>();
                Assert.Equal(65, foa.Value);
            }
        }
예제 #4
0
        public static void TestDllImportPseudoCustomAttribute()
        {
            TypeInfo runtimeType = typeof(DllImportHolders).GetTypeInfo();  // Intentionally not projected - using to get expected results.

            MethodInfo[] runtimeMethods = runtimeType.DeclaredMethods.OrderBy(m => m.Name).ToArray();

            TypeInfo ecmaType = runtimeType.Project().GetTypeInfo();

            MethodInfo[] ecmaMethods = ecmaType.DeclaredMethods.OrderBy(m => m.Name).ToArray();

            Assert.Equal(runtimeMethods.Length, ecmaMethods.Length);
            for (int i = 0; i < runtimeMethods.Length; i++)
            {
                DllImportAttribute  expected = runtimeMethods[i].GetCustomAttribute <DllImportAttribute>();
                CustomAttributeData cad      = ecmaMethods[i].CustomAttributes.Single(c => c.AttributeType.Name == nameof(DllImportAttribute));
                DllImportAttribute  actual   = cad.UnprojectAndInstantiate <DllImportAttribute>();
                AssertEqual(expected, actual);
            }
        }