使用“类型”来类型转换对象C# 中的对象
到目前为止,这对我来说有点棘手。我想知道是否可以使用 System.Type 对象对对象进行类型转换。
我在下面说明了我的意思:
public interface IDataAdapter
{
object Transform(object input);
Type GetOutputType();
}
public class SomeRandomAdapter : IDataAdapter
{
public object Transform(object input)
{
string output;
// Do some stuff to transform input to output...
return output;
}
public Type GetOutputType()
{
return typeof(string);
}
}
// Later when using the above methods I would like to be able to go...
var output = t.Transform(input) as t.GetOutputType();
上面是一个通用接口,这就是为什么我使用“对象”作为类型。
This one has proven to be a little tricky for me so far. I am wondering if it is possible to type cast an object using a System.Type object.
I have illustrated below what I mean:
public interface IDataAdapter
{
object Transform(object input);
Type GetOutputType();
}
public class SomeRandomAdapter : IDataAdapter
{
public object Transform(object input)
{
string output;
// Do some stuff to transform input to output...
return output;
}
public Type GetOutputType()
{
return typeof(string);
}
}
// Later when using the above methods I would like to be able to go...
var output = t.Transform(input) as t.GetOutputType();
The above is a generic interface which is why I am using "object" for the types.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
典型的方法是使用泛型,如下所示:
正如您在下面的评论中提到的,泛型与 C++ 模板非常相似。您可以在此处找到泛型的 MSDN 文档,以及文章“ C++ 模板和 C# 泛型之间的差异(C# 编程指南)”将可能会有帮助。
最后,我可能会误解您想要在方法体内执行的操作:我不确定如何将任意类型
T
转换为另一个任意类型T2
,除非您指定对泛型类型的约束。例如,您可能需要指定它们都必须实现某个接口。 类型参数的约束(C# 编程指南) 介绍了如何执行此操作在 C# 中。编辑:鉴于您修改后的问题,我认为Marco M. 的这个答案是正确的(也就是说,我认为您应该在当前尝试使用
Converter
委托的地方使用Converter
委托>IDataAdapter 接口。)The typical way to do that is to use generics, like so:
As you mentioned in your comment below, generics are very similar to C++ Templates. You can find the MSDN documentation for Generics here, and the article "Differences Between C++ Templates and C# Generics (C# Programming Guide)" will probably be helpful.
Finally, I might be misunderstanding what you want to do inside the method body: I'm not sure how you'll transform an arbitrary type
T
into another arbitrary typeT2
, unless you specify constraints on the generic types. For example, you might need to specify that they both have to implement some interface. Constraints on Type Parameters (C# Programming Guide) describes how to do this in C#.Edit: Given your revised question, I think this answer from Marco M. is correct (that is, I think you should use the
Converter
delegate where you're currently trying to use yourIDataAdapter
interface.)当你确定它返回一个字符串时,为什么要把它弄复杂呢?
如果我误解了你的意思,这里还有另一种方法
Why make it complicated, when you are sure that it returns a string?
If I have misunderstood what you are saying, here is one more way
您最好使用类似 Converter delegate 的东西
作为示例,请查看 msdn< /a>
You are better off using something like the Converter delegate
for an example, check out msdn
这就是我所采用的(基于 IEnumerable 结构):
This is what I have gone with (based off the IEnumerable structure):
使用实际的通用接口不是更有意义吗:
Would it not make more sense to use an actual generic interface: