gpt4 book ai didi

c# - 正确的数据类型用于类型映射?

转载 作者:行者123 更新时间:2023-11-30 16:00:47 24 4
gpt4 key购买 nike

我想实现一个类似于 AutoMapper 的基本映射系统,但都是显式映射,没有基于约定的映射。

为此,我编写了一个类,该类应该维护“映射”函数的寄存器并按需查找它们以便将一种类型映射到另一种类型。我设想的用法是这样的:

启动时的某处:

Mapper.Register<TypeA, TypeB>(typeA =>
{
var typeB = new TypeB()
{
Property1 = typeA.Property1
};

return typeB;
}

然后当我想执行映射时...

TypeA typeA = new TypeA();
TypeB typeB = Mapper.Map<TypeA, TypeB>(typeA);

目前我使用的是 Dictionary<Tuple<Type, Type>, Delegate>存储这个映射寄存器,但它没有像我希望的那样工作......

public static class Mapper
{
private readonly static Dictionary<Tuple<Type, Type>, Delegate> Mappings = new Dictionary<Tuple<Type, Type>, Delegate>();

public static void Register<TSource, TDestination>(Func<TSource, TDestination> mappingFunction)
where TSource : class
where TDestination : class
{
Delegate mappingFunc;

if (Mappings.TryGetValue(new Tuple<Type, Type>(typeof (TSource), typeof (TDestination)), out mappingFunc))
Mappings[new Tuple<Type, Type>(typeof (TSource), typeof (TDestination))] = mappingFunction;
else
Mappings.Add(new Tuple<Type, Type>(typeof (TSource), typeof (TDestination)), mappingFunction);
}

public static TDestination Map<TSource, TDestination>(TSource src)
where TSource : class
where TDestination : class
{
Delegate mappingFunc;

if (!Mappings.TryGetValue(new Tuple<Type, Type>(typeof (TSource), typeof (TDestination)), out mappingFunc))
throw new Exception("Invalid mapping: no mapping found for requested types.");

var func = mappingFunc as Func<TSource, TDestination>;

if (func == null)
throw new Exception("Invalid mapping: no mapping found for requested types.");

return func.Invoke(src);
}
}

当使用这段代码时,注册映射工作正常,但从字典中检索它们失败,我认为这是因为 Map 中的第三行。方法:

Mappings.TryGetValue(new Tuple<Type, Type>(typeof (TSource), typeof (TDestination)), out mappingFunc)

这一行总是过不了关,我如果我没理解错的话,是因为Tuple是引用类型,所以Tuple<Type, Type>的新实例,不管是什么Item1Item2是,将永远匹配 Dictionary 中的任何键.

所以,Dictionary<Tuple<Type, Type>, Delegate>不适合存储此映射寄存器。那么,数据类型是什么?

最佳答案

当我尝试运行上面的代码时,我得到了我期望的结果:

Mapper.Register<string, Regex>(s => new Regex("not using the given string"));
Mapper.Register<string, Regex>(s => new Regex(s));
var regex = Mapper.Map<string, Regex>(@"\w*");
// regex is now the Regex object instantiated with @"\w*"

换句话说,您的代码似乎运行正常。

This line always fails to pass the test, and I think if I understand correctly, it's because Tuple is a reference type, so a new instance of Tuple<Type, Type>, regardless of what Item1 and Item2 are, will never match any key in the Dictionary.

实际上,Tuple的执行确实支持你正在尝试做的事情。 Dictionary使用 GetHashCodeEquals引擎盖下的方法进行查找。 Equals方法通常检查对象的引用相等性,但 Tuple source code表明它被专门编程为使用结构相等:

    public override Boolean Equals(Object obj) {
return ((IStructuralEquatable) this).Equals(obj, EqualityComparer<Object>.Default);;
}

Boolean IStructuralEquatable.Equals(Object other, IEqualityComparer comparer) {
if (other == null) return false;

Tuple<T1, T2> objTuple = other as Tuple<T1, T2>;

if (objTuple == null) {
return false;
}

return comparer.Equals(m_Item1, objTuple.m_Item1) && comparer.Equals(m_Item2, objTuple.m_Item2);
}

A Dictionary是这个用例的正确方法,因为可以进行任意数量的注册,我们希望确保快速查找。但是,我认为我可能仍会创建自己的类型 TypeMapping用于 Dictionary而不是 Tuple<Type, Type> ,因为我认为元组不表达意图/用法。请记住覆盖 GetHashCodeEquals所以它可以与 Dictionary 一起正常运行像这样:

public class TypeMapping : IStructuralEquatable
{
public Type From { get; private set; }
public Type To { get; private set; }

public TypeMapping (Type from, Type to)
{
From = from;
To = to;
}

public override int GetHashCode()
{
return ((IStructuralEquatable) this).GetHashCode(EqualityComparer<Object>.Default);
}

int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
{
var hash = 17;
unchecked
{
hash = hash * 31 + From.GetHashCode();
hash = hash * 31 + To.GetHashCode();
}
return hash;
}

public override bool Equals(Object obj) {
return ((IStructuralEquatable) this).Equals(obj, EqualityComparer<Object>.Default);
}

bool IStructuralEquatable.Equals(Object other, IEqualityComparer comparer) {
if (other == null) return false;

var otherMapping = other as TypeMapping;
if (otherMapping == null) return false;

return comparer.Equals(From, otherMapping.From) && comparer.Equals(To, otherMapping.To);
}
}

然后你会得到你的 Dictionary在 Mapping 类中看起来像这样(对 RegistrationMap 方法进行了相应的更改):

private readonly static Dictionary<TypeMapping, Delegate> Mappings = new Dictionary<TypeMapping, Delegate>();

关于c# - 正确的数据类型用于类型映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39694570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com