简体   繁体   English

使用C#中的“Type”对象键入转换对象

[英]Type Casting an Object using a “Type” Object in C#

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. 我想知道是否可以使用System.Type对象键入强制转换对象。

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. 以上是一个通用接口,这就是我为类型使用“object”的原因。

The typical way to do that is to use generics, like so: 这样做的典型方法是使用泛型,如下所示:

public T2 Transform<T, T2>(T input)
{
    T2 output;

    // Do some stuff to transform input to output...

    return output;
}

int    number = 0;
string numberString = t.Transform<int, string>(number);

As you mentioned in your comment below, generics are very similar to C++ Templates. 正如您在下面的评论中提到的,泛型与C ++模板非常相似。 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. 您可以在此处找到GenericsMSDN文档 ,文章“ C ++模板与C#Generics之间的差异(C#编程指南) ”可能会有所帮助。

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 type T2 , unless you specify constraints on the generic types. 最后,我可能误解了你想在方法体内做什么:我不确定如何将任意类型T转换为另一个任意类型T2 ,除非你在泛型类型上指定约束。 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#. 类型参数的约束(C#编程指南)描述了如何在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 your IDataAdapter interface.) 编辑 :鉴于您修改过的问题,我认为Marco M.的答案是正确的(也就是说,我认为您应该使用当前尝试使用IDataAdapter接口的Converter委托。)

Why make it complicated, when you are sure that it returns a string? 当你确定它返回一个字符串时,为什么要使它变得复杂?

var output = t.Transform(input) as string;

If I have misunderstood what you are saying, here is one more way 如果我误解了你在说什么,这里还有一个方法

var output = Convert.ChangeType(t.Transform(input), t.GetOutputType());

You are better off using something like the Converter delegate 你最好使用像转换委托这样的东西

public delegate TOutput Converter<TInput, TOutput>(TInput input);

for an example, check out msdn 例如,看看msdn

public static void Main()
{
    // Create an array of PointF objects.
    PointF[] apf = {
        new PointF(27.8F, 32.62F),
        new PointF(99.3F, 147.273F),
        new PointF(7.5F, 1412.2F) };

    // Display each element in the PointF array.
    Console.WriteLine();
    foreach( PointF p in apf )
        Console.WriteLine(p);

    // Convert each PointF element to a Point object.
    Point[] ap = Array.ConvertAll(apf, 
        new Converter<PointF, Point>(PointFToPoint));

    // Display each element in the Point array.
    Console.WriteLine();
    foreach( Point p in ap )
    {
        Console.WriteLine(p);
    }
}

public static Point PointFToPoint(PointF pf)
{
    return new Point(((int) pf.X), ((int) pf.Y));
}

The above is a generic interface which is why I am using "object" for the types 以上是一个通用接口,这就是我为类型使用“object”的原因

Would it not make more sense to use an actual generic interface: 使用实际的通用接口是否更有意义:

public U Transform<T, U>(T input)
{
    string output;

    return output;
}

U output = t.Transform(input) as U;

This is what I have gone with (based off the IEnumerable structure): 这就是我所使用的(基于IEnumerable结构):

public interface IDataAdapter
{
    object Transform(object input);
}

public interface IDataAdapter<OutT, InT> : IDataAdapter
{
    OutT Transform(InT input);
}

public class SomeClass : IDataAdapter<string, string>
{
    public string Transform(string input)
    {
        // Do something...
    }

    public object Transform(object input)
    {
        throw new NotImplementedException();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM