locked
abstract class private instance method RRS feed

  • Question

  • Hi!

    I'd like to know if there is any trick to invoke abstract class private instance method?

     

    // abstract class


    using System;

    abstract class MainClass
    {
        string instanceMethod() 
        {      
            return "instanceMethod() in abstract MainClass";
        }

        static string staticMethod()
        {
            return "staticMethod() in abstract MainClass";
        }

        static void Main()
        {
            // Console.WriteLine(instanceMethod()); // ?
                                                          
            Console.WriteLine(staticMethod());                                                                                        
        }
    }

     

    Thanks

    Friday, October 12, 2007 6:15 PM

Answers

  • You can use this sample and also visit http://msdn2.microsoft.com/en-us/library/d8ba5k1h.aspx for more information.

     

    Code Block

    using System;

    using System.Reflection;

    using System.Windows.Forms;

    public class A

    {

    private int method()

    {

    return 0;

    }

    }

    public class B

    {

    private int method()

    {

    return 1;

    }

    }

    class Mymethodinfo

    {

    public static int Main()

    {

    Console.WriteLine("\nReflection.MethodInfo");

    A MyA = new A();

    B MyB = new B();

    // Get the Type and MethodInfo.

    Type MyTypea = Type.GetType("A");

    MethodInfo Mymethodinfoa = MyTypea.GetMethod("method");

    Type MyTypeb = Type.GetType("B");

    MethodInfo Mymethodinfob = MyTypeb.GetMethod("method");

    // Get and display the Invoke method.

    Console.Write("\nFirst method - " + MyTypea.FullName + " returns " + Mymethodinfoa.Invoke(MyA, null));

    Console.Write("\nSecond method - " + MyTypeb.FullName + " returns " + Mymethodinfob.Invoke(MyB, null));

    return 0;

    }

    }

     

     

    Regards,

    Eugene

    Monday, October 15, 2007 12:55 PM

All replies

  • Code Block

    using System;

    abstract class MainClass
    {
        string instanceMethod()
        {      
            return "instanceMethod() in abstract MainClass";
        }

        static string staticMethod()
        {
            return "staticMethod() in abstract MainClass";
        }

        static void Main()
        {
            MainClass mainClass = new Derived();
            Console.WriteLine(mainClass.instanceMethod());
                                                          
            Console.WriteLine(staticMethod());
        }
    }

    class Derived : MainClass { }

    Friday, October 12, 2007 6:22 PM
  • Hi,

     

    You can try using reflection to access it.

     

    Regards,

    Eugene

     

     

     

    Sunday, October 14, 2007 4:51 PM
  • Thanks for the reply. Can you give me an example?

    Monday, October 15, 2007 5:34 AM
  • Hi,

    What you want to do is not really object-orientation, are you sure you're not making a design mistake? The whole idea of having private methods is that they are not directly accessible from the outside and instance methods can only be invoked on an instance of the class. Abstract classes are used to be forced to create classes that inherit from it.

    So what you want to do is making a hack around these language features or do you want to experiment with reflection?


    Cheers,
    Steven Hillaert

    http://blog.hill-it.be
    Monday, October 15, 2007 12:43 PM
  • Yeah, I'd like to experiment!

    Monday, October 15, 2007 12:48 PM
  • You can use this sample and also visit http://msdn2.microsoft.com/en-us/library/d8ba5k1h.aspx for more information.

     

    Code Block

    using System;

    using System.Reflection;

    using System.Windows.Forms;

    public class A

    {

    private int method()

    {

    return 0;

    }

    }

    public class B

    {

    private int method()

    {

    return 1;

    }

    }

    class Mymethodinfo

    {

    public static int Main()

    {

    Console.WriteLine("\nReflection.MethodInfo");

    A MyA = new A();

    B MyB = new B();

    // Get the Type and MethodInfo.

    Type MyTypea = Type.GetType("A");

    MethodInfo Mymethodinfoa = MyTypea.GetMethod("method");

    Type MyTypeb = Type.GetType("B");

    MethodInfo Mymethodinfob = MyTypeb.GetMethod("method");

    // Get and display the Invoke method.

    Console.Write("\nFirst method - " + MyTypea.FullName + " returns " + Mymethodinfoa.Invoke(MyA, null));

    Console.Write("\nSecond method - " + MyTypeb.FullName + " returns " + Mymethodinfob.Invoke(MyB, null));

    return 0;

    }

    }

     

     

    Regards,

    Eugene

    Monday, October 15, 2007 12:55 PM