本文整理汇总了C#中ResolutionContext.CreateElementContext方法的典型用法代码示例。如果您正苦于以下问题:C# ResolutionContext.CreateElementContext方法的具体用法?C# ResolutionContext.CreateElementContext怎么用?C# ResolutionContext.CreateElementContext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ResolutionContext
的用法示例。
在下文中一共展示了ResolutionContext.CreateElementContext方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Map
public object Map(ResolutionContext context)
{
var runner = context.MapperContext.Runner;
if (context.IsSourceValueNull && runner.ShouldMapSourceCollectionAsNull(context))
{
return null;
}
var genericSourceDictType = context.SourceType.GetDictionaryType();
var sourceKeyType = genericSourceDictType.GetGenericArguments()[0];
var sourceValueType = genericSourceDictType.GetGenericArguments()[1];
var sourceKvpType = KvpType.MakeGenericType(sourceKeyType, sourceValueType);
var genericDestDictType = context.DestinationType.GetDictionaryType();
var destKeyType = genericDestDictType.GetGenericArguments()[0];
var destValueType = genericDestDictType.GetGenericArguments()[1];
var kvpEnumerator = GetKeyValuePairEnumerator(context, sourceKvpType);
var destDictionary = ObjectCreator.CreateDictionary(context.DestinationType, destKeyType, destValueType);
var count = 0;
while (kvpEnumerator.MoveNext())
{
var keyValuePair = kvpEnumerator.Current;
var sourceKey = sourceKvpType.GetProperty("Key").GetValue(keyValuePair, new object[0]);
var sourceValue = sourceKvpType.GetProperty("Value").GetValue(keyValuePair, new object[0]);
var keyTypeMap = runner.ConfigurationProvider.ResolveTypeMap(sourceKey, null, sourceKeyType,
destKeyType);
var valueTypeMap = runner.ConfigurationProvider.ResolveTypeMap(sourceValue, null, sourceValueType,
destValueType);
var keyContext = context.CreateElementContext(keyTypeMap, sourceKey, sourceKeyType,
destKeyType, count);
var valueContext = context.CreateElementContext(valueTypeMap, sourceValue, sourceValueType,
destValueType, count);
var destKey = runner.Map(keyContext);
var destValue = runner.Map(valueContext);
genericDestDictType.GetMethod("Add").Invoke(destDictionary, new[] {destKey, destValue});
count++;
}
return destDictionary;
}
示例2: Map
public object Map(ResolutionContext context, IMappingEngineRunner mapper)
{
if (context.IsSourceValueNull && mapper.ShouldMapSourceCollectionAsNull(context))
return null;
var sourceEnumerableValue = (IEnumerable) context.SourceValue ?? new object[0];
IEnumerable<object> keyValuePairs = sourceEnumerableValue.Cast<object>();
Type genericSourceDictType = context.SourceType.GetDictionaryType();
Type sourceKeyType = genericSourceDictType.GetGenericArguments()[0];
Type sourceValueType = genericSourceDictType.GetGenericArguments()[1];
Type sourceKvpType = KvpType.MakeGenericType(sourceKeyType, sourceValueType);
Type genericDestDictType = context.DestinationType.GetDictionaryType();
Type destKeyType = genericDestDictType.GetGenericArguments()[0];
Type destValueType = genericDestDictType.GetGenericArguments()[1];
var dictionaryEntries = keyValuePairs.OfType<DictionaryEntry>();
if (dictionaryEntries.Any())
keyValuePairs = dictionaryEntries.Select(e => Activator.CreateInstance(sourceKvpType, e.Key, e.Value));
object destDictionary = ObjectCreator.CreateDictionary(context.DestinationType, destKeyType, destValueType);
int count = 0;
foreach (object keyValuePair in keyValuePairs)
{
object sourceKey = sourceKvpType.GetProperty("Key").GetValue(keyValuePair, new object[0]);
object sourceValue = sourceKvpType.GetProperty("Value").GetValue(keyValuePair, new object[0]);
TypeMap keyTypeMap = mapper.ConfigurationProvider.ResolveTypeMap(sourceKey, null, sourceKeyType,
destKeyType);
TypeMap valueTypeMap = mapper.ConfigurationProvider.ResolveTypeMap(sourceValue, null, sourceValueType,
destValueType);
ResolutionContext keyContext = context.CreateElementContext(keyTypeMap, sourceKey, sourceKeyType,
destKeyType, count);
ResolutionContext valueContext = context.CreateElementContext(valueTypeMap, sourceValue, sourceValueType,
destValueType, count);
object destKey = mapper.Map(keyContext);
object destValue = mapper.Map(valueContext);
genericDestDictType.GetMethod("Add").Invoke(destDictionary, new[] {destKey, destValue});
count++;
}
return destDictionary;
}
示例3: Map
public object Map(ResolutionContext context)
{
var runner = context.MapperContext.Runner;
var sourceEnumerableValue = (IEnumerable)context.SourceValue ?? new object[0];
var enumerableValue = sourceEnumerableValue.Cast<object>();
var sourceElementType = TypeHelper.GetElementType(context.SourceType, sourceEnumerableValue);
var genericDestDictType = context.DestinationType.GetDictionaryType();
var destKeyType = genericDestDictType.GetGenericArguments()[0];
var destValueType = genericDestDictType.GetGenericArguments()[1];
var destKvpType = KvpType.MakeGenericType(destKeyType, destValueType);
var destDictionary = ObjectCreator.CreateDictionary(context.DestinationType, destKeyType, destValueType);
var count = 0;
foreach (var item in enumerableValue)
{
var typeMap = runner.ConfigurationProvider.ResolveTypeMap(item, null, sourceElementType, destKvpType);
var targetSourceType = typeMap != null ? typeMap.SourceType : sourceElementType;
var targetDestinationType = typeMap != null ? typeMap.DestinationType : destKvpType;
var newContext = context.CreateElementContext(typeMap, item, targetSourceType, targetDestinationType,
count);
var mappedValue = runner.Map(newContext);
var keyProperty = mappedValue.GetType().GetProperty("Key");
var destKey = keyProperty.GetValue(mappedValue, null);
var valueProperty = mappedValue.GetType().GetProperty("Value");
var destValue = valueProperty.GetValue(mappedValue, null);
genericDestDictType.GetMethod("Add").Invoke(destDictionary, new[] {destKey, destValue});
count++;
}
return destDictionary;
}