[英]Multiple generic types in same list and calling their methods
I'm making an object validation framework in my spare time to learn a few things and maybe use it for some school projects. 我在业余时间制作一个对象验证框架来学习一些东西,并可能将它用于一些学校项目。
I have my generic Rule class, which looks something like this : 我有我的通用Rule类,它看起来像这样:
class Rule<T>
{
string propertyName;
Func<T, bool> ruleLambda;
bool IsBroken(T value)
{
return ruleLambda(value);
}
}
An object that would be validated would look a bit like this : 将被验证的对象看起来有点像这样:
class Example
{
List<Rule<?>> MyRules; // can take all types of rules
List<Rule<T>> Validate<T>(string propertyName, T value)
{
List<Rule<T>> brokenRules = new List<Rule<T>>();
foreach (Rule rule in MyRules.Where(r => r.propertyName == propertyName))
{
if (rule.IsBroken(value))
brokenRules.Add(rule);
}
return brokenRules;
}
}
Where the T value
argument would be the value of one of the Example class's properties, which can be of any type. 其中
T value
参数是Example类的一个属性的值,可以是任何类型。
The Validate<T>
method is called whenever a property is set. 只要设置了属性,就会调用
Validate<T>
方法。
The problem lies with the class's list of rules. 问题在于班级的规则列表。 Specifically the
List<Rule<?>>
line above. 特别是上面的
List<Rule<?>>
行。 I want to store all the rules for a given class in the same list. 我想在同一个列表中存储给定类的所有规则。
Alas, C# doesn't have a wildcard for generic types like in Java. 唉,C#没有像Java那样的通用类型的通配符。
How should I do this? 我该怎么做?
A non-generic interface or base class utilizing objects instead of T could work, but how would I call the generic Rule's IsBroken
method and not the non-generic one? 使用对象而不是T的非泛型接口或基类可以工作,但是如何调用泛型规则的
IsBroken
方法而不是非泛型方法呢?
I would store your rules as object
inside the Example
class and use Enumerable.OfType<T>
to find the matching rules for a given type: 我将您的规则存储为
Example
类中的object
,并使用Enumerable.OfType<T>
来查找给定类型的匹配规则:
class Example
{
private List<object> rules;
List<Rule<T>> Validate<T>(string propertyName, T value)
{
return this.rules.OfType<Rule<T>>()
.Where(r => r.PropertyName == propertyName && r.IsBroken(value))
.ToList();
}
}
In cases where I've needed something like this, I use interfaces or non-generic base classes. 在我需要这样的东西的情况下,我使用接口或非泛型基类。 For example, you could create an interface:
例如,您可以创建一个接口:
public interface IRule
{
//non-generic properties & methods
}
public class Rule<T> : IRule
{
//implementation
}
then create a list of the interfaces: 然后创建一个接口列表:
private List<IRule> MyRules;
If you want to make converting from the interface to the generic easy, you could add an extension method: 如果要从界面转换为通用easy,可以添加扩展方法:
public static Rule<T> ToGeneric<T>(this IRule rule)
{
return rule as Rule<T>;
}
I've tried a few things and I've found something that works pretty well for my needs. 我尝试了一些东西,并且我发现了一些适合我需求的东西。 I have
Rule<T>
inherit from a base abstract rule class, with a generic IsBroken
method: 我有一个
Rule<T>
从基本抽象规则类继承,使用通用的IsBroken
方法:
abstract class Rule
{
string propertyName;
Func<object, bool> objectRule;
bool IsBroken<T>(T value)
{
Rule<T> rule = this as Rule<T>;
if (rule == null)
return objectRule(value);
return rule.IsBroken(value);
}
}
As you can see, I try to convert the base class to its generic counterpart using the generic type parameter in the IsBroken
method. 如您所见,我尝试使用
IsBroken
方法中的泛型类型参数将基类转换为其通用对应项。
Also, when creating a Rule<T>
instance, I send a Func<object, bool>
to its base class protected constructor: 此外,在创建
Rule<T>
实例时,我将Func<object, bool>
发送到其基类保护构造函数:
public Rule(string propertyName, Func<T, bool> ruleLambda)
: base(propertyName, ConvertToObjectFunc(ruleLambda))
{
}
With the conversion method looking like this: 转换方法如下所示:
static Func<object, bool> ConvertToObjectFunc(Func<T, bool> func)
{
return new Func<object, bool>(o => func((T)o));
}
However, if it can't cast o to type T, it crashes. 但是,如果它无法将o转换为类型T,则会崩溃。 So I wrote this... thing:
所以我写了这个...东西:
static Func<object, bool> ConvertToObjectFunc(Func<T, bool> func)
{
return new Func<object, bool>
(
o =>
{
try
{
T obj = (T)o;
return func(obj);
}
catch { return true; } // rule is broken by default
}
);
}
It's pretty ugly, but it works. 这很难看,但它确实有效。 Hope this can help anybody else.
希望这可以帮助其他人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.