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


C# CodeVariableDeclarationStatement.Assign方法代码示例

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


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

示例1: CreateToSourceAssignmentMethod

		public static CodeStatement CreateToSourceAssignmentMethod(
			TranslateAttribute attr,
			CodeVariableDeclarationStatement toSource,
			PropertyInfo toSourceProperty, CodeParameterDeclarationExpression fromTargetParam,
			Func<Type, Type, TranslateAttribute> getTypesTranslateAttributeFn)
		{

			var fromTargetProperty = attr.TargetType.GetProperty(toSourceProperty.Name);
			var cantReadFromTarget = fromTargetProperty.GetGetMethod() == null;
			if (cantReadFromTarget)
			{
				return new CodeCommentStatement(string.Format("Skipping property 'to.{0}' because 'model.{1}' is write-only",
					toSourceProperty.Name, fromTargetProperty.Name));
			}
			var cantWriteToSource = toSourceProperty.GetSetMethod() == null;
			if (cantWriteToSource)
			{
				return new CodeCommentStatement(string.Format("Skipping property 'to.{0}' because 'to.{1}' is read-only",
					toSourceProperty.Name, toSourceProperty.Name));
			}

			//to[property.Name] = this[property.Name] e.g:
			//	to.Name = from.Name;
			if (fromTargetProperty.PropertyType.IsAssignableFrom(toSourceProperty.PropertyType))
			{
				return toSource.Assign(
					toSource.RefProperty(toSourceProperty.Name),
					fromTargetParam.Name.RefArgument().RefProperty(toSourceProperty.Name));
			}

			//to[property.Name] = from[property.PropertyType.Name].ToTarget() e.g:
			//to.Address = from.Address.ToTarget();

			var toSourcePropertyType = toSourceProperty.PropertyType;
			var fromTargetPropertyType = fromTargetProperty.PropertyType;
			var targetAttr = getTypesTranslateAttributeFn(toSourcePropertyType, fromTargetPropertyType);
			var useExtensionMethods = attr is TranslateExtensionAttribute;
			var isTargetTranslatableAlso = targetAttr != null;

			if (isTargetTranslatableAlso)
			{
				var toSourceMethodName = targetAttr.GetConvertToSourceMethodName();
				var method = useExtensionMethods
									? fromTargetParam.RefProperty(toSourceProperty.Name).Call(toSourceMethodName)
									: toSourcePropertyType.Call(toSourceMethodName, fromTargetParam.RefProperty(toSourceProperty.Name));
				return toSource.Assign(toSourceProperty.Name, method);
			}

			var fromSourceIsGenericList = toSourcePropertyType.IsGenericType && toSourcePropertyType.GetGenericTypeDefinition() == typeof(List<>);
			var toTargetIsGenericList = fromTargetPropertyType.IsGenericType && fromTargetPropertyType.GetGenericTypeDefinition() == typeof(List<>);
			var bothAreGenericLists = fromSourceIsGenericList && toTargetIsGenericList;

			if (bothAreGenericLists)
			{
				//to.PhoneNumbers = from.PhoneNumbers.ToPhoneNumbers();
				var propertyListItemTypeAttr = getTypesTranslateAttributeFn(toSourcePropertyType.GetGenericArguments()[0], fromTargetPropertyType.GetGenericArguments()[0]);
				var sourceIsTranslatable = propertyListItemTypeAttr != null;
				if (sourceIsTranslatable)
				{
					var toSourcesMethodName = propertyListItemTypeAttr.GetConvertToSourcesMethodName();
					var method = useExtensionMethods
					             		? fromTargetParam.RefProperty(fromTargetProperty.Name).Call(toSourcesMethodName)
					             		: propertyListItemTypeAttr.SourceType.Call(toSourcesMethodName, fromTargetParam.RefProperty(fromTargetProperty.Name));

					return toSource.RefProperty(toSourceProperty.Name).Assign(method);
				}
			}

			//to[property.Name] = this[property.Name].ToString() e.g:
			//	to.Name = from.Name;
			if (toSourceProperty.PropertyType == typeof(string)
				&& StringConverterUtils.CanCreateFromString(fromTargetProperty.PropertyType))
			{
				var fromTargetPropertyRef = fromTargetParam.Name.RefArgument().RefProperty(toSourceProperty.Name);
				return fromTargetPropertyRef.IfIsNotNull(
							toSource.Assign(
								toSource.RefProperty(toSourceProperty.Name),
								fromTargetPropertyRef.Call("ToString")));
			}

			// 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"
				, toSourceProperty.PropertyType.Namespace, toSourceProperty.PropertyType.Name, toSourceProperty.Name
				, fromTargetProperty.PropertyType.Namespace, fromTargetProperty.PropertyType.Name, fromTargetProperty.Name));
		}
开发者ID:nuxleus,项目名称:ServiceStack.Extras,代码行数:85,代码来源:TranslatorClassGenerator.cs


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