Provides functionalities for property conversion.
상속: IPropertyConverter
예제 #1
0
 public void ConvertTestComplex()
 {
     var target = new PropertyConverter();
     var source = new TestClassA {InnerClass = new TestClassC()};
     const string expected = "test";
     source.InnerClass.Name = expected;
     var fromProperty = typeof (TestClassA).GetProperty("InnerClass");
     var targetObject = new TestClassB {InnerClassName = "wrongstring"};
     var toProperty = typeof (TestClassB).GetProperty("InnerClassName");
     target.Convert(source, fromProperty, targetObject, toProperty);
     Assert.AreEqual(expected, targetObject.InnerClassName);
     Assert.AreEqual(expected, source.InnerClass.Name);
 }
예제 #2
0
 public void ConvertTestSimple()
 {
     var target = new PropertyConverter();
     var source = new TestClassA();
     const string expected = "test";
     source.Name = expected;
     var fromProperty = typeof (TestClassA).GetProperty("Name");
     var targetObject = new TestClassB();
     targetObject.Name = "wrongstring";
     var toProperty = typeof (TestClassB).GetProperty("Name");
     target.Convert(source, fromProperty, targetObject, toProperty);
     Assert.AreEqual(expected, targetObject.Name);
     Assert.AreEqual(expected, source.Name);
 }
예제 #3
0
 public void ConvertTestComplex()
 {
     PropertyConverter target = new PropertyConverter();
     TestClassA source = new TestClassA();
     source.InnerClass = new TestClassC();
     var expected = "test";
     source.InnerClass.Name = expected;
     PropertyInfo fromProperty = typeof(TestClassA).GetProperty("InnerClass");
     TestClassB targetObject = new TestClassB();
     targetObject.InnerClassName = "wrongstring";
     PropertyInfo toProperty = typeof(TestClassB).GetProperty("InnerClassName");
     target.Convert(source, fromProperty, targetObject, toProperty);
     Assert.AreEqual(expected, targetObject.InnerClassName);
     Assert.AreEqual(expected, source.InnerClass.Name);
 }
예제 #4
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="ReflectionPropertyMappingInfo&lt;TSource, TTarget&gt;" /> class.
 /// </summary>
 /// <param name="sourceProperty">The source property.</param>
 /// <param name="targetProperty">The target property.</param>
 /// <param name="strict">if set to <c>true</c>, performs a strict mapping.</param>
 /// <param name="converter">The property converter to be used.</param>
 public ReflectionPropertyMappingInfo(
     PropertyInfo sourceProperty,
     PropertyInfo targetProperty,
     bool strict,
     PropertyConverter converter)
 {
     Guard.CheckArgumentNotNull(sourceProperty, "sourceMemberName");
     Guard.CheckArgumentNotNull(targetProperty, "targetMemberName");
     Guard.CheckArgumentNotNull(converter, "converter");
     SourceMemberName   = sourceProperty.Name;
     TargetMemberName   = targetProperty.Name;
     SourcePropertyInfo = sourceProperty;
     TargetPropertyInfo = targetProperty;
     Converter          = converter;
     Strict             = strict;
 }
예제 #5
0
        protected Expression GetExpression(
            string targetMemberName,
            ReflectionPropertyMappingInfo <TSource, TTarget> reflectionInfo,
            ParameterExpression sourceParameter,
            ParameterExpression targetParameter)
        {
            Guard.CheckArgumentNotNull(reflectionInfo, "reflectionInfo");

            var converter = new PropertyConverter();

            return(converter.CreateConvertExpression(
                       reflectionInfo.SourcePropertyInfo,
                       reflectionInfo.TargetPropertyInfo,
                       sourceParameter,
                       targetParameter));
        }
예제 #6
0
        public void CreateConvertExpression_MultipleCases_ConvertsCorrectly(
            Type sourceType,
            Type targetType,
            string sourcePropertyName,
            string targetPropertyName,
            object sourcePropertyValue)
        {
            var target = new PropertyConverter();
            var fixture = new Fixture();
            MethodInfo createMethod = fixture.GetType().GetMethod("CreateAnonymous");
            object sourceObject = Activator.CreateInstance(sourceType);
            object targetObject = Activator.CreateInstance(targetType);
            ParameterExpression sourceParam = Expression.Parameter(sourceType);
            ParameterExpression targetParam = Expression.Parameter(targetType);
            PropertyInfo sourceProp = sourceType.GetProperty(sourcePropertyName);
            PropertyInfo targetProp = targetType.GetProperty(targetPropertyName);
            if (sourcePropertyValue != null)
            {
                sourceProp.SetValue(sourceObject, sourcePropertyValue, null);
            }

            var expr = target.CreateConvertExpression(
                sourceProp,
                targetProp,
                sourceParam,
                targetParam);

            var actionType = typeof (MappingAction<,>).MakeGenericType(sourceType, targetType);
            var lambda = Expression.Lambda(actionType, expr, sourceParam, targetParam);
            var action = lambda.Compile();
            action.DynamicInvoke(sourceObject, targetObject);
            var resultProp = targetProp.GetValue(targetObject, null);
            resultProp.ShouldBe(sourcePropertyValue);
        }
예제 #7
0
 private void TestMatch(string fromProp, string toProp, bool expected)
 {
     var target = new PropertyConverter();
     var from = typeof (TestClassA).GetProperty(fromProp);
     var to = typeof (TestClassB).GetProperty(toProp);
     var actual = target.CanConvert(@from, to);
     Assert.AreEqual(expected, actual);
 }
예제 #8
0
 public void PropertyConverterNegativeTest()
 {
     var target = new PropertyConverter();
     var source = new TestClassA();
     var sourceProperty = source.GetType().GetProperty("InnerClass");
     var result = new TestClassB();
     var resultProperty = result.GetType().GetProperty("InnerClassCode");
     source.InnerClass = new TestClassC();
     target.Convert(
         source,
         sourceProperty,
         target,
         resultProperty,
         false);
 }