예제 #1
0
        public object GetValue(object instance)
        {
            if (null == this._valueGetter)
            {
                if (Monitor.TryEnter(this))
                {
                    try
                    {
                        if (null == this._valueGetter)
                        {
                            this._valueGetter = DelegateGenerator.CreateValueGetter(this.Definition.Property);
                        }
                    }
                    finally
                    {
                        Monitor.Exit(this);
                    }
                }
                else
                {
                    return(this.Definition.Property.GetMemberValue(instance));
                }
            }

            return(this._valueGetter(instance));
        }
예제 #2
0
        void Init()
        {
            ConstructorInfo constructor     = this.ConstructorInfo;
            InstanceCreator instanceCreator = DelegateGenerator.CreateInstanceCreator(constructor);

            this.InstanceCreator = instanceCreator;
        }
예제 #3
0
        public void SetValue(object instance, object value)
        {
            if (null == this._valueSetter)
            {
                if (Monitor.TryEnter(this))
                {
                    try
                    {
                        if (null == this._valueSetter)
                            this._valueSetter = DelegateGenerator.CreateValueSetter(this.Definition.Property);
                    }
                    finally
                    {
                        Monitor.Exit(this);
                    }
                }
                else
                {
                    this.Definition.Property.SetMemberValue(instance, value);
                    return;
                }
            }

            this._valueSetter(instance, value);
        }
        public void TestGeneratePropertyGetter()
        {
            var actual = new TestClass
            {
                A = "aaa",
                B = new NestedClass {Data = "data"},
                C = DateTime.Now,
                D = 42m,
                E = 54.0,
                F = 666,
                G = Guid.NewGuid(),
                H = new [] { "a", "b", "c"}
            };

            var aGetter = DelegateGenerator.GeneratePropertyGetter(actual.GetType().GetProperty("A"));
            var bGetter = DelegateGenerator.GeneratePropertyGetter(actual.GetType().GetProperty("B"));
            var cGetter = DelegateGenerator.GeneratePropertyGetter(actual.GetType().GetProperty("C"));
            var dGetter = DelegateGenerator.GeneratePropertyGetter(actual.GetType().GetProperty("D"));
            var eGetter = DelegateGenerator.GeneratePropertyGetter(actual.GetType().GetProperty("E"));
            var fGetter = DelegateGenerator.GeneratePropertyGetter(actual.GetType().GetProperty("F"));
            var gGetter = DelegateGenerator.GeneratePropertyGetter(actual.GetType().GetProperty("G"));
            var hGetter = DelegateGenerator.GeneratePropertyGetter(actual.GetType().GetProperty("H"));

            aGetter(actual).Should().Be(actual.A);
            bGetter(actual).Should().Be(actual.B);
            cGetter(actual).Should().Be(actual.C);
            dGetter(actual).Should().Be(actual.D);
            eGetter(actual).Should().Be(actual.E);
            fGetter(actual).Should().Be(actual.F);
            gGetter(actual).Should().Be(actual.G);
            hGetter(actual).Should().Be(actual.H);
        }
예제 #5
0
        public override object GetValue(object instance)
        {
            if (null == this._valueGetter)
            {
                if (Monitor.TryEnter(this))
                {
                    try
                    {
                        if (null == this._valueGetter)
                        {
                            this._valueGetter = DelegateGenerator.CreateValueGetter(this._fieldInfo);
                        }
                    }
                    finally
                    {
                        Monitor.Exit(this);
                    }
                }
                else
                {
                    return(this._fieldInfo.GetValue(instance));
                }
            }

            return(this._valueGetter(instance));
        }
예제 #6
0
        void Init()
        {
            Type t       = this.Type;
            var  members = t.GetMembers(BindingFlags.Public | BindingFlags.Instance);

            Dictionary <MemberInfo, IMRM> mappingMemberMappers = new Dictionary <MemberInfo, IMRM>();
            Dictionary <MemberInfo, Action <object, object> > complexMemberSetters = new Dictionary <MemberInfo, Action <object, object> >();

            foreach (var member in members)
            {
                Type         memberType = null;
                PropertyInfo prop       = null;
                FieldInfo    field      = null;

                if ((prop = member as PropertyInfo) != null)
                {
                    if (prop.GetSetMethod() == null)
                    {
                        continue;//对于没有公共的 setter 直接跳过
                    }
                    memberType = prop.PropertyType;
                }
                else if ((field = member as FieldInfo) != null)
                {
                    memberType = field.FieldType;
                }
                else
                {
                    continue;//只支持公共属性和字段
                }
                MappingTypeInfo mappingTypeInfo;
                if (MappingTypeSystem.IsMappingType(memberType, out mappingTypeInfo))
                {
                    IMRM mrm = MRMHelper.CreateMRM(member, mappingTypeInfo);
                    mappingMemberMappers.Add(member, mrm);
                }
                else
                {
                    if (prop != null)
                    {
                        Action <object, object> valueSetter = DelegateGenerator.CreateValueSetter(prop);
                        complexMemberSetters.Add(member, valueSetter);
                    }
                    else if (field != null)
                    {
                        Action <object, object> valueSetter = DelegateGenerator.CreateValueSetter(field);
                        complexMemberSetters.Add(member, valueSetter);
                    }
                    else
                    {
                        continue;
                    }

                    continue;
                }
            }

            this._mappingMemberMappers = Utils.Clone(mappingMemberMappers);
            this._complexMemberSetters = Utils.Clone(complexMemberSetters);
        }
예제 #7
0
        void Init()
        {
            Type t       = this.Type;
            var  members = t.GetMembers(BindingFlags.Public | BindingFlags.Instance);

            Dictionary <MemberInfo, IMRM> mappingMemberMappers = new Dictionary <MemberInfo, IMRM>();
            Dictionary <MemberInfo, Action <object, object> > complexMemberSetters = new Dictionary <MemberInfo, Action <object, object> >();

            foreach (var member in members)
            {
                if (!member.HasPublicSetter())
                {
                    continue;
                }

                //只支持公共属性和字段
                Type memberType = member.GetMemberType();

                MappingTypeInfo mappingTypeInfo;
                if (MappingTypeSystem.IsMappingType(memberType, out mappingTypeInfo))
                {
                    IMRM mrm = MRMHelper.CreateMRM(member, mappingTypeInfo);
                    mappingMemberMappers.Add(member, mrm);
                }
                else
                {
                    Action <object, object> valueSetter = DelegateGenerator.CreateValueSetter(member);
                    complexMemberSetters.Add(member, valueSetter);
                }
            }

            this._mappingMemberMappers = PublicHelper.Clone(mappingMemberMappers);
            this._complexMemberSetters = PublicHelper.Clone(complexMemberSetters);
        }
예제 #8
0
        public override void SetValue(object instance, object value)
        {
            if (null == this._valueSetter)
            {
                if (Monitor.TryEnter(this))
                {
                    try
                    {
                        if (null == this._valueSetter)
                        {
                            this._valueSetter = DelegateGenerator.CreateValueSetter(this._fieldInfo);
                        }
                    }
                    finally
                    {
                        Monitor.Exit(this);
                    }
                }
                else
                {
                    this._fieldInfo.SetValue(instance, value);
                    return;
                }
            }

            this._valueSetter(instance, value);
        }
예제 #9
0
    public void TestGenerateDelegateEvents()
    {
        var gen         = new CodeUnitGenerator("TestCodeGen");
        var classGen    = new ClassGenerator("TestClass");
        var delegateGen = new DelegateGenerator("MyEventHandler")
                          .AddParameter("TestClass", "myRef")
                          .AddReturnType(typeof(bool));

        var eventGen = new EventGenerator("OnSomeTrigger", delegateGen.delegateType);

        classGen.AddEvent(eventGen);
        var fireEventMethod = new MethodGenerator("FireEvent")
                              .AddStatement(new StatementBuilder()
                                            //.AddSnippetExpression("Debug.Log();DoMoreStuff();"));
                                            .InvokeEvent(eventGen, new ParamBuilder()
                                                         .AddPrimitiveExpression("new TestClass()")));

        classGen.AddMethod(fireEventMethod);

        gen.AddType(delegateGen);
        gen.AddType(classGen);

        var classSubscriber = new ClassGenerator("MySubscribeClass");
        var field           = new FieldGenerator("TestClass", "eventSource");

        classSubscriber.AddField(field);

        var constructor = new ConstructorGenerator(classSubscriber.classType);

        classSubscriber.AddMethod(constructor);

        var eventHandler = new MethodGenerator("OnSomeTrigger", delegateGen)
                           .AddStatement(new StatementBuilder()
                                         .AddSnippet("Debug.Log(\"Expression1\");")
                                         .AddSnippet("Debug.Log(\"Expression2\");"));

        var subscribeMethod = new MethodGenerator("AddListener")
                              .AddStatement(new StatementBuilder()
                                            .AttachEvent(eventHandler, new FieldTarget(field), eventGen));

        classSubscriber.AddMethod(
            new MethodGenerator("Unsubscribe").AddStatement(
                new StatementBuilder()
                .DetachEvent(eventHandler, new FieldTarget(field), eventGen)));
        classSubscriber.AddMethod(eventHandler);
        classSubscriber.AddMethod(subscribeMethod);
        gen.AddType(classSubscriber);

        var ccu = gen.GenerateCompileUnit();

        var output = StringCompiler.CompileToString(ccu);

        //Debug.Log(output);
        Assert.IsTrue(output.Contains("OnSomeTrigger"));
        Assert.IsTrue(output.Contains("FireEvent"));
        Assert.IsTrue(output.Contains("+="));
        Assert.IsTrue(output.Contains("-="));
        Assert.IsTrue(output.Contains("delegate"));
        Assert.IsTrue(output.Contains("event"));
    }
 internal FieldDescriptor GetFieldDescriptor(FieldInfo fieldInfo, SyncFlags syncFlags)
 {
     return(new FieldDescriptor(
                GenerateFieldFactoryByType(fieldInfo.FieldType, syncFlags),
                DelegateGenerator.GenerateGetter(fieldInfo),
                DelegateGenerator.GenerateSetter(fieldInfo)
                ));
 }
 internal FieldDescriptor GetFieldDescriptor(PropertyInfo propertyInfo, SyncFlags syncFlags)
 {
     return(new FieldDescriptor(
                GenerateFieldFactoryByType(propertyInfo.PropertyType, syncFlags),
                DelegateGenerator.GenerateGetter(propertyInfo),
                DelegateGenerator.GenerateSetter(propertyInfo)
                ));
 }
예제 #12
0
        public void DefaultDelegateSpecificationIsCorrect()
        {
            // Arrange
            // Act
            var sut = new DelegateGenerator();

            // Assert
            Assert.IsType <DelegateSpecification>(sut.Specification);
        }
예제 #13
0
        public static MemberValueGetter GetMemberValueGetter(MemberInfo memberInfo)
        {
            MemberValueGetter getter = Cache.GetOrAdd(memberInfo, member =>
            {
                return(DelegateGenerator.CreateValueGetter(member));
            });

            return(getter);
        }
예제 #14
0
 public ColumnPropertyMapper(string columnName, PropertyInfo propertyInfo)
 {
     ColumnName   = columnName;
     PropertyInfo = propertyInfo;
     Name         = propertyInfo.Name;
     Type         = propertyInfo.PropertyType;
     _getMethod   = DelegateGenerator.GetDelegate(propertyInfo);
     _setMethod   = DelegateGenerator.SetDelegate(propertyInfo);
 }
예제 #15
0
        public void CreateWithNullContainerThrowsArgumentNullException()
        {
            // Arrange
            var dummyRequest = new object();
            var sut          = new DelegateGenerator();

            // Act & assert
            Assert.Throws <ArgumentNullException>(() => sut.Create(dummyRequest, null));
        }
예제 #16
0
        public void SutIsSpecimenBuilder()
        {
            // Arrange
            // Act
            var sut = new DelegateGenerator();

            // Assert
            Assert.IsAssignableFrom <ISpecimenBuilder>(sut);
        }
예제 #17
0
        public void CustomSpecificationIsPreserved()
        {
            // Arrange
            var specification = new TrueRequestSpecification();
            // Act
            var sut = new DelegateGenerator(specification);

            // Assert
            Assert.Equal(specification, sut.Specification);
        }
예제 #18
0
        public void SutIsSpecimenBuilder()
        {
            // Fixture setup
            // Exercise system
            var sut = new DelegateGenerator();

            // Verify outcome
            Assert.IsAssignableFrom <ISpecimenBuilder>(sut);
            // Teardown
        }
예제 #19
0
        public void CreateWithNullContainerThrowsArgumentNullException()
        {
            // Fixture setup
            var dummyRequest = new object();
            var sut          = new DelegateGenerator();

            // Exercise system and verify outcome
            Assert.Throws(typeof(ArgumentNullException), () => sut.Create(dummyRequest, null));
            // Teardown
        }
예제 #20
0
        public void CreateWithNullRequestReturnsNoSpecimen()
        {
            // Arrange
            var sut = new DelegateGenerator();
            // Act
            var dummyContainer = new DelegatingSpecimenContext();
            var result         = sut.Create(null, dummyContainer);

            // Assert
            Assert.Equal(new NoSpecimen(), result);
        }
예제 #21
0
        public void CreateWithReturnObjectDoubleObjectParametersDelegateRequestReturnsCorrectResult()
        {
            // Arrange
            var delegateRequest = typeof(Func <object, object, object>);
            var sut             = new DelegateGenerator();
            // Act
            var dummyContainer = new DelegatingSpecimenContext();
            var result         = sut.Create(delegateRequest, dummyContainer);

            // Assert
            Assert.IsType <Func <object, object, object> >(result);
        }
예제 #22
0
        public void CreateWithVoidParameterlessDelegateRequestReturnsDelegateNotThrowing()
        {
            // Arrange
            var delegateRequest = typeof(Action);
            var sut             = new DelegateGenerator();
            // Act
            var dummyContainer = new DelegatingSpecimenContext();
            var result         = sut.Create(delegateRequest, dummyContainer);

            // Assert
            Assert.Null(Record.Exception(() => ((Action)result).Invoke()));
        }
예제 #23
0
        public void CreateWithVoidParameterlessDelegateRequestReturnsCorrectResult()
        {
            // Arrange
            var delegateRequest = typeof(Action);
            var sut             = new DelegateGenerator();
            // Act
            var dummyContainer = new DelegatingSpecimenContext();
            var result         = sut.Create(delegateRequest, dummyContainer);

            // Assert
            Assert.IsType <Action>(result);
        }
예제 #24
0
        public void CreateWithNullRequestReturnsNoSpecimen()
        {
            // Fixture setup
            var sut = new DelegateGenerator();
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();
            var result         = sut.Create(null, dummyContainer);

            // Verify outcome
            Assert.Equal(new NoSpecimen(), result);
            // Teardown
        }
예제 #25
0
        public void CreateWithVoidDoubleObjectParametersDelegateRequestReturnsCorrectResult()
        {
            // Fixture setup
            var delegateRequest = typeof(Action <object, object>);
            var sut             = new DelegateGenerator();
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();
            var result         = sut.Create(delegateRequest, dummyContainer);

            // Verify outcome
            Assert.IsType <Action <object, object> >(result);
            // Teardown
        }
예제 #26
0
        public void CreateWithVoidParameterlessDelegateRequestReturnsDelegateNotThrowing()
        {
            // Fixture setup
            var delegateRequest = typeof(Action);
            var sut             = new DelegateGenerator();
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();
            var result         = sut.Create(delegateRequest, dummyContainer);

            // Verify outcome
            Assert.DoesNotThrow(() => ((Action)result).Invoke());
            // Teardown
        }
예제 #27
0
        public void CreateWithNonDelegateRequestReturnsNoSpecimen()
        {
            // Arrange
            var nonDelegateRequest = new object();
            var sut = new DelegateGenerator();
            // Act
            var dummyContainer = new DelegatingSpecimenContext();
            var result         = sut.Create(nonDelegateRequest, dummyContainer);
            // Assert
            var expectedResult = new NoSpecimen();

            Assert.Equal(expectedResult, result);
        }
예제 #28
0
        public void CreateWithReturnValueParameterlessDelegateRequestReturnsCorrectResult()
        {
            // Fixture setup
            var delegateRequest = typeof(Func <int>);
            var sut             = new DelegateGenerator();
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();
            var result         = sut.Create(delegateRequest, dummyContainer);

            // Verify outcome
            Assert.IsType <Func <int> >(result);
            // Teardown
        }
예제 #29
0
        public void CreateWithNonDelegateRequestReturnsNoSpecimen()
        {
            // Fixture setup
            var nonDelegateRequest = new object();
            var sut = new DelegateGenerator();
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();
            var result         = sut.Create(nonDelegateRequest, dummyContainer);
            // Verify outcome
            var expectedResult = new NoSpecimen();

            Assert.Equal(expectedResult, result);
            // Teardown
        }
예제 #30
0
        public IEnumerable <NamespaceDeclarationSyntax> Create(ICompilation compilation)
        {
            IEnumerable <(ITypeDefinition typeDefinition, bool isAbstract, IEnumerable <IMethod> methods)> values = compilation.GetPublicTypeDefinitions()
                                                                                                                    .Where(
                x => x.Kind != TypeKind.Interface &&
                (!IsMulticastDelegateDerived(x) ||
                 !x.DirectBaseTypes.Any()) &&
                !_garbageTypeList.Any(y => x.FullName.Contains(y)) &&
                CocoaDelegateNames.Any(cocoaName => x.FullName.EndsWith(cocoaName, StringComparison.OrdinalIgnoreCase)))
                                                                                                                    .Select(typeDef => new { TypeDefinition = typeDef, IsAbstract = typeDef.Methods.Any(method => method.ReturnType.FullName != RoslynHelpers.VoidType && method.IsAbstract) })
                                                                                                                    .Select(x => (x.TypeDefinition, x.IsAbstract, GetPublicDelegateMethods(x.TypeDefinition)))
                                                                                                                    .Where(x => x.Item3.Any());

            return(DelegateGenerator.Generate(values));
        }