Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Reflection;
- using System.Runtime.InteropServices;
- namespace ConsoleApplication1
- {
- [StructLayout(LayoutKind.Explicit)]
- public struct CatOrDog
- {
- [FieldOffset(0)]
- public Cat Cat;
- [FieldOffset(0)]
- public Dog Dog;
- }
- class Program
- {
- static void Main(string[] args)
- {
- var catOrDog = new CatOrDog { Cat = new Cat { Name = "Jenny" } };
- catOrDog.Dog.Say();
- }
- }
- public class Cat
- {
- public string Name { get; set; }
- public void Say()
- {
- Console.WriteLine("Meow " + Name);
- }
- }
- public class Dog
- {
- public string Name { get; set; }
- public void Say()
- {
- Console.WriteLine("Woof" + Name);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement