使用“类型”来类型转换对象C# 中的对象

发布于 2024-08-03 07:43:44 字数 650 浏览 11 评论 0原文

到目前为止,这对我来说有点棘手。我想知道是否可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

辞旧 2024-08-10 07:43:44

典型的方法是使用泛型,如下所示:

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);

正如您在下面的评论中提到的,泛型与 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:

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. 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 type T2, 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 your IDataAdapter interface.)

浅唱ヾ落雨殇 2024-08-10 07:43:44

当你确定它返回一个字符串时,为什么要把它弄复杂呢?

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

如果我误解了你的意思,这里还有另一种方法

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

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());
下雨或天晴 2024-08-10 07:43:44

您最好使用类似 Converter delegate 的东西

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

作为示例,请查看 msdn< /a>

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));
}

You are better off using something like the Converter delegate

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

for an example, check out 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));
}
唱一曲作罢 2024-08-10 07:43:44

这就是我所采用的(基于 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();
    }
}

This is what I have gone with (based off the IEnumerable structure):

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();
    }
}
少女净妖师 2024-08-10 07:43:44

上面是一个通用接口,这就是为什么我使用“对象”作为类型

使用实际的通用接口不是更有意义吗:

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

    return output;
}

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

The above is a generic interface which is why I am using "object" for the types

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文