在C#C ++中的reinterpret_cast的等效 [英] Equivalent of C++'s reinterpret_cast in C#

查看:665
本文介绍了在C#C ++中的reinterpret_cast的等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道什么的C ++的的reinterpret_cast 在C#!?

I wonder what's the equivalent of C++'s reinterpret_cast in C#!?

下面是我的示例:

class Base
{
    protected int counter = 0;
}

class Foo : Base
{
    public int Counter
    {
        get { return counter; }
    }
}

Base b = new Base();
Foo f = b as Foo; // f will be null



我没有异议,为什么˚F将是无效的,因为它应该的。但如果是C ++,我可以写美孚F = reinterpret_cast的<富>(二); ,并得到我想要的东西。我能做些什么,以实现在C#中的一样吗?

I've got no objection why f will be null since it should be. But if it was C++ I could have wrote Foo f = reinterpret_cast<Foo>(b); and get what I wanted. What can I do to achieve the same in C#?

PS。我假设基本是一致的数据,明智的。

PS. I'm assuming that Base and Foo are consistent data-wise.

[更新]

下面是一个简单的场景,其中一个的reinterpret_cast 可能会有所帮助:

Here's a simple scenario where a reinterpret_cast could be helpful:

考虑写一个XXX-RPC库,你已经得到了传入的参数也没有控制服务调用签名。您的图书馆应该具有给定参数来调用服务问道。如果C#支持的reinterpret_cast 我可以简单地的reinterpret_cast 给定参数注入预期的人,并调用该服务。

Consider writing a XXX-RPC library where you've got no control over the incoming parameters nor the signature of services to call. Your library is supposed to call the asked service with the given parameters. If C# supported reinterpret_cast I could simply reinterpret_cast the given parameters into the expected ones and call the service.

推荐答案

由于一些答案指出,.NET是强制型严格安全的问题的范围。 A 的reinterpret_cast 将是一个固有的不安全的操作,从而实现一个是,要么通过可能的方式的反射系列化的的,而这两个相关

discussion

As some of the answers point out, .Net is enforcing type safety rigorously in the question's scope. A reinterpret_cast would be an inherently unsafe operation, hence the possible ways to implement one would be either through reflection or serialization, whereas the two are related.

正如在更新所提到的,一个可能的用途可能是一个RPC框架。 RPC库通常使用序列/反射无论如何,有几个可用的人的:

As you mentioned in an update, a possible use could be an RPC framework. RPC libraries typically use serialization/reflection anyway, and there are a couple of usable ones:

  • protobuf-remote
  • msgpack-rpc-cli

所以,你可能不希望自己写一个,也许吧。

so, you might not want to write one yourself, perhaps.

如果你的类基本将使用公共属性,你可以使用 AutoMapper

If your class Base would use public properties, you could use AutoMapper:

class Base
{
    public int Counter { get; set; }
    // ...
}



...

...

AutoMapper.Mapper.CreateMap<Base, Foo>();
Foo foo = AutoMapper.Mapper.Map<Foo>(b);



其中,不需要从派生基本可言。它只是让你有兴趣映射到该属性。但同样,你可能不需要两种类型都 - 架构的重新思考可能是解决方案

Where Foo need not be derived from Base at all. It just has to have the property you are interested in mapping onto. But again, you might not need two types at all - a rethinking of the architecture might be the solution.

通常情况下,就没有必要使用的reinterpret_cast ,由一个干净的架构,能装进在.Net框架中使用的模式的方式。如果你仍然坚持有类似的东西,这里是使用小型序列化库 protobuf网的解决方案< 。/ A>

Typically, there is no need to use reinterpret_cast, by way of a clean architecture that fits nicely into the patterns used in the .Net Framework. If you still insist on having something like that, here's a solution using the compact serialization library protobuf-net.

您的类:

using System;
using System.IO;
using ProtoBuf;
using ProtoBuf.Meta;

[ProtoContract]
[ProtoInclude(3, typeof(Foo))]
class Base
{
    [ProtoMember(1)]
    protected int counter = 0;

    public Base(int c) { counter = c; }
    public Base() { }
}

[ProtoContract]
class Foo : Base
{
    public int Counter { get { return counter; } }
}

和一个可运行的序列化反序列化的例子:

and a runnable serialization-deserialization example:

class Program
{
    static void Main(string[] args)
    {
        Base b = new Base(33);
        using (MemoryStream stream = new MemoryStream())
        {
            Serializer.Serialize<Base>(stream, b);
            Console.WriteLine("Length: {0}", stream.Length);
            stream.Seek(0, SeekOrigin.Begin);
            Foo f=new Foo();
            RuntimeTypeModel.Default.Deserialize(stream, f, typeof(Foo));
            Console.WriteLine("Foo: {0}", f.Counter);
        }
    }
}



输出

outputting

Length: 2
Foo: 33

如果你不想在你的合同申报派生类型,看的这个例子...

If you don't want to declare derived types in your contract, see this example...

正如你所见,序列化是非常。紧凑

As you see, the serialization is extremely compact.

如果你想使用更多的领域,你可以尝试领域的隐含序列化:

If you want to use more fields, you might try implicit serialization of fields:

[ProtoContract(ImplicitFields = ImplicitFields.AllFields)]

一个普通的的reinterpret_cast 绝对是可能的或者通过该序列化的解决方案,或者直接通过反射来实现,但我不会在目前投入的时间。

A generic reinterpret_cast might definitely be possible to implement either via this serialization solution, or directly via reflection, but I wouldn't invest the time at the moment.

这篇关于在C#C ++中的reinterpret_cast的等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆