Esempio n. 1
0
        public void Constructor_PassAccessors()
        {
            foreach (var memberAndAccessorsInfo in GetMembersAndAccessors(typeof(LazyMemberTestClass)))
            {
                MemberInfo   member     = memberAndAccessorsInfo.Item1;
                MemberTypes  memberType = memberAndAccessorsInfo.Item2.Item1;
                MemberInfo[] accessors  = memberAndAccessorsInfo.Item2.Item2;

                LazyMemberInfo lazy = new LazyMemberInfo(memberType, accessors);
                Assert.Equal(memberType, lazy.MemberType);
                Assert.Equal(accessors.Length, lazy.GetAccessors().Length);
                Assert.True(accessors.SequenceEqual(lazy.GetAccessors()));
            }
        }
Esempio n. 2
0
        public void Constructor_PassAccessors()
        {
            foreach (var memberAndAccessorsInfo in GetMembersAndAccessors(typeof(LazyMemberTestClass)))
            {
                MemberInfo member = memberAndAccessorsInfo.Item1;
                MemberTypes memberType = memberAndAccessorsInfo.Item2.Item1;
                MemberInfo[] accessors = memberAndAccessorsInfo.Item2.Item2;

                LazyMemberInfo lazy = new LazyMemberInfo(memberType, accessors);
                Assert.AreEqual(memberType, lazy.MemberType);
                Assert.AreEqual(accessors.Length, lazy.GetAccessors().Length);
                Assert.IsTrue(accessors.SequenceEqual(lazy.GetAccessors()));
            }
        }
        public static ReflectionMember ToReflectionMember(this LazyMemberInfo lazyMember)
        {
            MemberInfo[] accessors  = lazyMember.GetAccessors();
            MemberTypes  memberType = lazyMember.MemberType;

            switch (memberType)
            {
            case MemberTypes.Field:
                if (accessors.Length != 1)
                {
                    throw new Exception(SR.Diagnostic_InternalExceptionMessage);
                }
                return(((FieldInfo)accessors[0]).ToReflectionField());

            case MemberTypes.Property:
                if (accessors.Length != 2)
                {
                    throw new Exception(SR.Diagnostic_InternalExceptionMessage);
                }
                return(ReflectionExtensions.CreateReflectionProperty((MethodInfo)accessors[0], (MethodInfo)accessors[1]));

            case MemberTypes.NestedType:
            case MemberTypes.TypeInfo:
                return(((Type)accessors[0]).ToReflectionType());

            default:
                if (memberType != MemberTypes.Method)
                {
                    throw new Exception(SR.Diagnostic_InternalExceptionMessage);
                }
                return(((MethodInfo)accessors[0]).ToReflectionMethod());
            }
        }
Esempio n. 4
0
        public void Constructor_PassInvalidAccessorCreators()
        {
            foreach (var memberAndAccessorsInfo in GetMembersAndAccessors(typeof(LazyMemberTestClass)))
            {
                MemberInfo   member     = memberAndAccessorsInfo.Item1;
                MemberTypes  memberType = memberAndAccessorsInfo.Item2.Item1;
                MemberInfo[] accessors  = memberAndAccessorsInfo.Item2.Item2;

                foreach (MemberTypes wrongMemberType in GetValidMemberTypes())
                {
                    if (wrongMemberType == memberType)
                    {
                        continue;
                    }
                    LazyMemberInfo lazy = new LazyMemberInfo(wrongMemberType, () => accessors);
                    ExceptionAssert.Throws <InvalidOperationException>(() =>
                    {
                        lazy.GetAccessors();
                    });
                }
            }
        }
Esempio n. 5
0
        public static ReflectionMember ToReflectionMember(this LazyMemberInfo lazyMember)
        {
            MemberInfo[] accessors  = lazyMember.GetAccessors();
            MemberTypes  memberType = lazyMember.MemberType;

            switch (memberType)
            {
            case MemberTypes.Field:
                Assumes.IsTrue(accessors.Length == 1);
                return(((FieldInfo)accessors[0]).ToReflectionField());

            case MemberTypes.Property:
                Assumes.IsTrue(accessors.Length == 2);
                return(ReflectionExtensions.CreateReflectionProperty((MethodInfo)accessors[0], (MethodInfo)accessors[1]));

            case MemberTypes.NestedType:
            case MemberTypes.TypeInfo:
                return(((Type)accessors[0]).ToReflectionType());

            default:
                Assumes.IsTrue(memberType == MemberTypes.Method);
                return(((MethodInfo)accessors[0]).ToReflectionMethod());
            }
        }
Esempio n. 6
0
        public void Constructor_PassInvalidAccessorCreators()
        {
            foreach (var memberAndAccessorsInfo in GetMembersAndAccessors(typeof(LazyMemberTestClass)))
            {
                MemberInfo member = memberAndAccessorsInfo.Item1;
                MemberTypes memberType = memberAndAccessorsInfo.Item2.Item1;
                MemberInfo[] accessors = memberAndAccessorsInfo.Item2.Item2;

                foreach (MemberTypes wrongMemberType in GetValidMemberTypes())
                {
                    if (wrongMemberType == memberType)
                    {
                        continue;
                    }
                    LazyMemberInfo lazy = new LazyMemberInfo(wrongMemberType, () => accessors);
                    ExceptionAssert.Throws<InvalidOperationException>(() =>
                    {
                        lazy.GetAccessors();
                    });
                }
            }
        }
 private static SerializableExportDefinition CreateTypeExport(
     ExportDefinition export,
     LazyMemberInfo memberInfo,
     Func<Type, TypeIdentity> identityGenerator)
 {
     Debug.Assert(memberInfo.GetAccessors().Count() == 1, "Only expecting one accessor for a type export.");
     Debug.Assert(memberInfo.GetAccessors().First() is Type, "Expecting the export to be a Type.");
     return TypeBasedExportDefinition.CreateDefinition(
         export.ContractName,
         memberInfo.GetAccessors().First() as Type,
         identityGenerator);
 }
 private static SerializableExportDefinition CreatePropertyExport(
     ExportDefinition export,
     LazyMemberInfo memberInfo,
     Func<Type, TypeIdentity> identityGenerator)
 {
     // this is really ugly because we assume that the underlying methods for a property are named as:
     // get_PROPERTYNAME and set_PROPERTYNAME. In this case we assume that exports always
     // have a get method.
     var getMember = memberInfo.GetAccessors().First(m => m.Name.Contains("get_"));
     var name = getMember.Name.Substring("get_".Length);
     var property = getMember.DeclaringType.GetProperty(name);
     return PropertyBasedExportDefinition.CreateDefinition(
         export.ContractName,
         property,
         identityGenerator);
 }