我想为通用方法创建一个垫片。但在那种情况下,我对 Generic 有点麻烦。
这是我的例子:
class BaseRepository <T> where T: Entity
{
public T[] FindAll()
{
return Method<T>.FindAll()
}
}
class ClassA : base<A>
{
}
class A : Entity
{
}
class ClassB : base<B>
{
}
class B : Entity
{
}
现在我想为 ClassA 和 ClassB 创建一个 ShimMethod
ShimBaseRepository<A>.AllInstances.FindAll = (repo) => MethodA();
ShimBaseRepository<B>.AllInstances.FindAll = (repo) => MethodB();
public A MethodA()
{
//Make the Same as MethodB
}
public B MethodB()
{
//Make the Same as MethodA
}
但是如果我有超过 20 个“基础”类怎么办?我不想为每个基类创建委托(delegate)/方法。我试过这样的事情:
List<Type> allEntityClasses = (from x in Assembly.GetAssembly(typeof(Entity)).GetTypes()
where !x.IsAbstract && !x.IsInterface
select x).ToList();
foreach(Type type in allEntityClasses=
{
ShimBaseRepository<type????>.AllInstances.FindAll = (repo) => Method();
}
public Entity????? Method()
{
}
在我的单元测试中,我将使用以下方法:
ClassA.FindAll()
ClassB.FindAll()
而不是:
Base.FindAll()
编辑: 我使用 Microsoft Fakes,所以我无法更改 ShimClass 中的任何内容。这是 Shim 生成的源代码。
public class ShimBaseRepository<T> : ShimBase<BaseRepository<T>> where T : Entity
{
public static class AllInstances
{
public static FakesDelegates.Func<BaseRepository<T>, T[]> FindAll { [ShimMethod("FindAll", 20)] set; }
}
}
我的意图是,我不想为每个实体创建委托(delegate),我只想遍历所有 EntityClass 并动态创建委托(delegate)。但是我不知道如何在
ShimBase<T>
最佳答案
好吧,让我们稍微讨论一下。 首先,这是一个使用虚方法的直接解决方案:
public class Base<T> where T : Entity
{
public virtual T[] FindAll()
{
return null;
}
}
然后重写FindAll
在具体类(class)中
或者,如果可以的话,制作 Base
摘要和InnerFindAll
也很抽象。
但是,如果您需要在运行时指定委托(delegate)(我可以看到您有一个特定的 Helper,但我不明白,为什么您在 Base 中调用 helper,然后您有一些未定义的问题 AllInstances 带有Func) 这种方法无济于事。您需要实现 Strategy pattern在 Base 中分配了一些默认策略。那么你将有 3 种方法来“解决”具体类中的策略:
- 在具体类的构造函数中硬编码一个策略
- 通过 DI 容器向具体类构造函数注入(inject)策略
- 实现某种映射器,它会为您返回适当的 EntityType 策略 (
T
)
此外,我认为您在设计方面存在一些问题。我看不出有任何理由需要将 FindAll 实现为注入(inject) Func<T>
类型静态属性的 lambda (是的,我认为您可以将 AllInstances.FindAll
替换为 static FindAll
)。所以如果我是你,我会使用抽象方法..
编辑
现在我遇到了你的问题,只能通过反射给你一个相当丑陋的解决方案......我不建议你使用它,因为它真的很严格
public class Program
{
static void Main(string[] args)
{
List<Type> allEntityClasses = (from x in Assembly.GetAssembly(typeof(Entity))
.GetTypes().Where(t=>typeof(Entity).IsAssignableFrom(t))
where !x.IsAbstract && !x.IsInterface
select x).ToList();
foreach (var type in allEntityClasses)
{
var genericType = typeof(BaseGeneric<>).MakeGenericType(type);
var helper = new DelegateHelper();
var myLambda = helper.GetLambdaForType(type);
var allInst = genericType.GetProperty("AllInstances").GetValue(null);
if (allInst == null)
{
allInst = Activator.CreateInstance(genericType.GetProperty("AllInstances").PropertyType);
}
allInst.GetType().GetProperty("FindAll").SetValue(allInst,myLambda);
}
}
}
public static class BaseGeneric<T>
{
public static AllInstances<T> AllInstances { get; set; }
}
public class AllInstances<T>
{
public Func<T[]> FindAll { get; set; }
}
public class DelegateHelper
{
public Delegate GetLambdaForType(Type type)
{
var funcType = typeof(Func<>).MakeGenericType(type.MakeArrayType());
var methodInfo = typeof(DelegateHelper).GetMethods().FirstOrDefault(t => t.Name == "FunctionMethod")
.MakeGenericMethod(type);
var @delegate = methodInfo.CreateDelegate(funcType, this);
return @delegate;
}
public T[] FunctionMethod<T>()
{
return new T[10];
}
}
public class Entity
{
}
public class EntityFirst
{
}
public class EntitySecond
{
}
关于c# - Shim 和通用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29093896/