带字符串参数的构造函数的通用约束?
如何创建一个泛型类,该类允许具有采用一个字符串参数的构造函数并实现 ToString 和实现如下两个函数的类型。
class Convert<T>:ConverterBase
where T:new()
{
public override object StringToField(string from)
{
try
{
return new T(from);
}
catch (ArgumentException exception)
{
ThrowConvertException(from, exception.Message);
return null;
}
}
public override string FieldToString(object from)
{
return from.ToString();
}
}
笔记: ConvertBase是FileHelpers csv阅读器库中的一个抽象类。我已经有了与 csv 中的字段相对应的类,不想创建继承 ConvertBase 的单独类以便与 FileHelpres 库一起使用。
How to create a generic class that allows types which has constructor taking one string argument and implements ToString and implements Two functions as below.
class Convert<T>:ConverterBase
where T:new()
{
public override object StringToField(string from)
{
try
{
return new T(from);
}
catch (ArgumentException exception)
{
ThrowConvertException(from, exception.Message);
return null;
}
}
public override string FieldToString(object from)
{
return from.ToString();
}
}
Note:
ConvertBase is a abstract class in FileHelpers csv reader library. I already have classes that corresponds to my fields in csv, didn't want to create seperate Classes that inherit ConvertBase inorder to use with the FileHelpres library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
无法使用构造函数约束 (
new()
) 来约束除空构造函数之外的任何内容。解决这个问题的一种方法是使用 lambda 表达式。例如,现在我可以创建
Convert
的实例,并使用 lambda 表达式转发到EDIT C# 2.0 实现类型的构造函数,因为 OP 仍停留在 2.0 上
There is no way to use the constructor constraint (
new()
) to constrain to anything other than an empty constructor. One way to work around this though is to use a lambda expression. For exampleNow I can create instances of
Convert<T>
and use a lambda expression to forward to the constructor of the typeEDIT C# 2.0 implementation since the OP is stuck with 2.0
您好,您可以创建一个基类来存储转换器函数并实现两个成员,之后您可以继承它并仅提供字符串到对象的转换
稍后创建一些简单的派生类例如:
希望这有帮助:)记得下载最后一个稳定版本来自:
http://teamcity.codebetter.com /viewLog.html?buildId=lastSuccessful&buildTypeId=bt66&tab=artifacts&guest=1
编辑:使用反射你可以尝试:
注意:反射很慢,所以你必须采取它考虑到
Hi there you can create a base class that stores the converter function and implements both members, after that you can inherit from it and only provide the string to object convertion
Later create some simple derived classes for example:
Hope this helps :) Remember to download the last stable version from:
http://teamcity.codebetter.com/viewLog.html?buildId=lastSuccessful&buildTypeId=bt66&tab=artifacts&guest=1
EDIT: Using Reflection you can try:
NOTE: Reflection is slow, so you must take that into account
你不能。您只能创建一个约束来强制您的类具有无参数构造函数。如您所知,其约束是
new()
。ToString()
可用于每种类型,因为它是在Object
上定义的,并且每种类型都继承自Object
。但是,您无法知道或强制执行您的类型是否提供自己的ToString()
实现。为了确保类型参数实现两个特定的方法,将它们放入一个接口中并将类型参数约束到该接口,并使您的类实现该接口。
You can't. You can only create a constraint that enforces, that your class has a parameterless constructor. The constraint for that is
new()
, as you know.ToString()
is available on every type, because it is defined onObject
and every type inherits fromObject
. However, you can't know or enforce, whether your type provides it's own implementation ofToString()
.To make sure, the type parameter implements two specific methods, put them into an interface and constrain the type parameter to that interface and make your class implement that interface.