locked
How can I extend a wrapper class to a struct with methods of the struct? RRS feed

  • Question

  • I was wondering if there was a way to do this.

    Basically I have three structs by the name of S1, S2, and S3.  They are related by have no inheritance structure.  I want to create IStructWrapper which has a class StructClass1, StructClass2, and StructClass3 which implement the IStructWrapper to give it a hierarchical structure.  This is easy, and I just have a reference to each struct in the class.  What I really want is to call methods on teh StructClass1 itself as thought it were the struct.  Is there any way to extend methods from a struct onto a class?

    Monday, February 2, 2009 2:23 AM

Answers

  •  Hi,

    I don't think you can do it the way you mean to since struct does allow inheritance or be inherited.

    You can write a method reference to the struct method in the class the call the class method instead.

    Cheers
    Binze
    • Marked as answer by Bin-ze Zhao Monday, February 9, 2009 2:59 AM
    Thursday, February 5, 2009 3:25 AM
  • You'd have to manually proxy the calls. Something like this :

    struct S1
    {
        public void FS1()
        {
            Console.WriteLine("S1.FS1");
        }
    }

    struct S2
    {
        public void FS2()
        {
            Console.WriteLine("S2.FS2");
        }
    }

    interface ICommon
    {
        void Foo();
    }

    class C1 : ICommon
    {
        public S1 S { get; private set; }

        public C1(S1 s)
        {
            this.S = s;
        }

        #region ICommon Members

        public void Foo()
        {
            this.S.FS1();
        }

        #endregion
    }

    class C2 : ICommon
    {
        public S2 S { get; private set; }

        public C2(S2 s)
        {
            this.S = s;
        }

        #region ICommon Members

        public void Foo()
        {
            this.S.FS2();
        }

        #endregion
    }

    private void Test()
    {
        S1 s1 = new S1();
        S2 s2 = new S2();

        C1 c1 = new C1(s1);
        C2 c2 = new C2(s2);

        c1.Foo();
        c2.Foo();
    }

    http://blog.voidnish.com
    • Marked as answer by Bin-ze Zhao Monday, February 9, 2009 2:59 AM
    Thursday, February 5, 2009 5:16 PM
    Moderator

All replies

  •  Hi,

    I don't think you can do it the way you mean to since struct does allow inheritance or be inherited.

    You can write a method reference to the struct method in the class the call the class method instead.

    Cheers
    Binze
    • Marked as answer by Bin-ze Zhao Monday, February 9, 2009 2:59 AM
    Thursday, February 5, 2009 3:25 AM
  • You'd have to manually proxy the calls. Something like this :

    struct S1
    {
        public void FS1()
        {
            Console.WriteLine("S1.FS1");
        }
    }

    struct S2
    {
        public void FS2()
        {
            Console.WriteLine("S2.FS2");
        }
    }

    interface ICommon
    {
        void Foo();
    }

    class C1 : ICommon
    {
        public S1 S { get; private set; }

        public C1(S1 s)
        {
            this.S = s;
        }

        #region ICommon Members

        public void Foo()
        {
            this.S.FS1();
        }

        #endregion
    }

    class C2 : ICommon
    {
        public S2 S { get; private set; }

        public C2(S2 s)
        {
            this.S = s;
        }

        #region ICommon Members

        public void Foo()
        {
            this.S.FS2();
        }

        #endregion
    }

    private void Test()
    {
        S1 s1 = new S1();
        S2 s2 = new S2();

        C1 c1 = new C1(s1);
        C2 c2 = new C2(s2);

        c1.Foo();
        c2.Foo();
    }

    http://blog.voidnish.com
    • Marked as answer by Bin-ze Zhao Monday, February 9, 2009 2:59 AM
    Thursday, February 5, 2009 5:16 PM
    Moderator