Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>discussion</h2> <p>As some of the answers point out, .Net is enforcing type safety rigorously in the question's scope. A <code>reinterpret_cast</code> would be an inherently unsafe operation, hence the possible ways to implement one would be either through <em>reflection</em> or <em>serialization</em>, whereas the two are related.</p> <p>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:</p> <ul> <li><a href="https://code.google.com/p/protobuf-remote/" rel="nofollow">protobuf-remote</a></li> <li><a href="https://github.com/yfakariya/msgpack-rpc-cli/wiki/Quick-start" rel="nofollow">msgpack-rpc-cli</a></li> </ul> <p>so, you might not want to write one yourself, perhaps.</p> <p>If your class <code>Base</code> would use public properties, you could use <a href="https://github.com/AutoMapper/AutoMapper/wiki/Getting-started" rel="nofollow">AutoMapper</a>:</p> <pre><code>class Base { public int Counter { get; set; } // ... } </code></pre> <p>...</p> <pre><code>AutoMapper.Mapper.CreateMap&lt;Base, Foo&gt;(); Foo foo = AutoMapper.Mapper.Map&lt;Foo&gt;(b); </code></pre> <p>Where <code>Foo</code> need not be derived from <code>Base</code> 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.</p> <p>Typically, there is no need to use <code>reinterpret_cast</code>, 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 <a href="https://code.google.com/p/protobuf-net/" rel="nofollow">protobuf-net</a>.</p> <h2>serialization solution</h2> <p>Your classes:</p> <pre><code>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; } } } </code></pre> <p>and a runnable serialization-deserialization example:</p> <pre><code>class Program { static void Main(string[] args) { Base b = new Base(33); using (MemoryStream stream = new MemoryStream()) { Serializer.Serialize&lt;Base&gt;(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); } } } </code></pre> <p>outputting</p> <pre><code>Length: 2 Foo: 33 </code></pre> <p>If you don't want to declare derived types in your contract, see <a href="https://github.com/e-travel/Protobuf-net-Examples/blob/master/ProtobufNet.Learning.Tests.Integration/Lesson6Inheritance.cs" rel="nofollow">this example...</a></p> <p>As you see, the serialization is extremely compact.</p> <p>If you want to use more fields, you might try implicit serialization of fields:</p> <pre><code>[ProtoContract(ImplicitFields = ImplicitFields.AllFields)] </code></pre> <p>A generic <code>reinterpret_cast</code> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload