当前位置: 首页>>代码示例>>C#>>正文


C# CodeParameterDeclarationExpression.Assign方法代码示例

本文整理汇总了C#中System.CodeDom.CodeParameterDeclarationExpression.Assign方法的典型用法代码示例。如果您正苦于以下问题:C# CodeParameterDeclarationExpression.Assign方法的具体用法?C# CodeParameterDeclarationExpression.Assign怎么用?C# CodeParameterDeclarationExpression.Assign使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.CodeDom.CodeParameterDeclarationExpression的用法示例。


在下文中一共展示了CodeParameterDeclarationExpression.Assign方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateToTargetAssignmentMethod

		public static CodeStatement CreateToTargetAssignmentMethod(
			TranslateAttribute attr,
			CodeParameterDeclarationExpression toTarget,
			PropertyInfo sourceProperty,
			CodeVariableReferenceExpression fromSourceVar,
			Func<Type, Type, TranslateAttribute> getTypesTranslateAttributeFn)
		{

			//model.Name = this.Name;
			var targetProperty = attr.TargetType.GetProperty(sourceProperty.Name);
			var cantWriteToTarget = targetProperty.GetSetMethod() == null;
			if (cantWriteToTarget)
			{
				return new CodeCommentStatement(string.Format("Skipping property 'model.{0}' because 'model.{1}' is read-only",
					targetProperty.Name, sourceProperty.Name));
			}
			var cantReadFromSource = sourceProperty.GetGetMethod() == null;
			if (cantReadFromSource)
			{
				return new CodeCommentStatement(string.Format("Skipping property 'model.{0}' because 'this.{1}' is write-only",
					targetProperty.Name, sourceProperty.Name));
			}

			var areBothTheSameTypes = targetProperty.PropertyType.IsAssignableFrom(sourceProperty.PropertyType);
			if (areBothTheSameTypes)
			{
				return toTarget.Assign(sourceProperty.Name, fromSourceVar.RefProperty(sourceProperty.Name));
			}

			//model.BillingAddress = this.BillingAddress.ToTarget();
			var sourcePropertyType = sourceProperty.PropertyType;
			var targetPropertyType = targetProperty.PropertyType;
			var sourceAttr = getTypesTranslateAttributeFn(sourcePropertyType, targetPropertyType);
			var isSourceTranslatableAlso = sourceAttr != null;
			var useExtensionMethods = attr is TranslateExtensionAttribute;

			if (isSourceTranslatableAlso)
			{
				var toTargetMethodName = sourceAttr.GetConvertToTargetMethodName();
				var method = fromSourceVar.RefProperty(sourceProperty.Name).Call(toTargetMethodName);
				
				return fromSourceVar.RefProperty(sourceProperty.Name).IfIsNotNull(
					toTarget.Assign(sourceProperty.Name, method)
				);
			}

			var sourceIsGenericList = sourcePropertyType.IsGenericType && sourcePropertyType.GetGenericTypeDefinition() == typeof(List<>);
			var targetIsGenericList = targetPropertyType.IsGenericType && targetPropertyType.GetGenericTypeDefinition() == typeof(List<>);
			var bothAreGenericLists = sourceIsGenericList && targetIsGenericList;

			if (bothAreGenericLists)
			{
				//to.PhoneNumbers = from.PhoneNumbers.ToPhoneNumbers();
				var propertyListItemTypeAttr = getTypesTranslateAttributeFn(sourcePropertyType.GetGenericArguments()[0], targetPropertyType.GetGenericArguments()[0]);
				var sourceIsTranslatable = propertyListItemTypeAttr != null;
				if (sourceIsTranslatable)
				{
					var toTargetsMethodName = propertyListItemTypeAttr.GetConvertToTargetsMethodName();
					var method = useExtensionMethods
					             		? fromSourceVar.RefProperty(sourceProperty.Name).Call(toTargetsMethodName)
					             		: propertyListItemTypeAttr.SourceType.Call(toTargetsMethodName, sourceProperty.Name.ThisProperty());
					
					return toTarget.Assign(sourceProperty.Name, method);
				}
			}

			//model.Type = StringConverterUtils.Parse<Target.PhoneNumberType>(this.Type);
			var sourcePropertyIsStringAndTargetIsConvertible = sourceProperty.PropertyType == typeof(string)
				&& StringConverterUtils.CanCreateFromString(targetProperty.PropertyType);
			if (sourcePropertyIsStringAndTargetIsConvertible)
			{
				//model.CardType = StringConverterUtils.Parse<CardType>(this.CardType);
				var methodResult = typeof(StringConverterUtils).CallGeneric(CONVERTER_PARSE_METHOD,
					new[] { targetProperty.PropertyType.GenericDefinition() },
					fromSourceVar.RefProperty(sourceProperty.Name));

				return toTarget.Assign(sourceProperty.Name.ThisProperty(), methodResult);
			}

			// Converting 'System.Collections.Generic.List`1 PhoneNumbers' to 'System.Collections.Generic.List`1 PhoneNumbers' is unsupported
			return new CodeCommentStatement(string.Format("Converting '{0}.{1} {2}' to '{3}.{4} {5}' is unsupported"
				, sourceProperty.PropertyType.Namespace, sourceProperty.PropertyType.Name, sourceProperty.Name
				, targetProperty.PropertyType.Namespace, targetProperty.PropertyType.Name, targetProperty.Name));
		}
开发者ID:nuxleus,项目名称:ServiceStack.Extras,代码行数:84,代码来源:TranslatorClassGenerator.cs


注:本文中的System.CodeDom.CodeParameterDeclarationExpression.Assign方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。