Sorry for talking against your design, but I really don't like it. :p
Shared methods can be inherited. If you look at Object.ReferenceEquals, it's
a shared method that is inherited in all classes in the framework. In the
constructor of your many classes, you can set a property of your base class
(that property with be "Protected").
> No. It's not possible to inherit shared members (methods, functions,
> Properties) from a base class.
> And a call to GeneralClass.SharedMethod is not enough because the method
has
> to handle with a variable which is defined in the inherited Class
> A call like GeneralClass.SharedMethod(VariableOfInheritedClass) is not
> wanted in my project, because this can irritate the user of my classes.
> It's very easy to call the common shared methods:
> a = ChildClass1.CommonSharedMethods.MethodA
> b = ChildClass1.CommonSharedMethods.MethodB
> c = ChildClass2.CommonSharedMethods.MethodA
> c = ChildClass2.CommonSharedMethods.MethodB
> This works and it is OK for me. What i want to do now is to call this
> methods with reflection. I will try your suggestion with the fieldinfo.
> I think, i will get it work, so my problem is solved for now.
> Thanx
> > oh...
> > and what are you inheriting from? If you're inheriting from a built-in
> > class, you could have your general class(called GeneralClass) inherit
from
> > it, and define the 25 methods there, and then all your other classes
would
> > inherit from GeneralClass. Would that work?
> > > I have 25 functions, properties, subs, which must be static.
> > > The methods implementing almost the same code, the only difference is
> that
> > > they handle a variable containing a different string for each class
> > > (classname of an other application).
> > > I guess that i will have somewere in the future over 500 classes.
> > > 500 classes x 25 shared members x 3 lines of code = 37500 lines of
code
> > > 500 classes x 1 Shared Property + (25 x 3 lines of code) = 575 lines
of
> > code
> > > Do you see the difference ? ;-)
> > > > Why don't you declare the shared function in each class, but call a
> > > > "generic" procedure to do the work? You would have only 3 lines of
> code
> > > per
> > > > function per class, and it would be MUCH easier to use your classes.
> > > > > Etienne,
> > > > > i will try your solution with the fieldinfo. I think this will
work.
> > > > > > This is called Object Oriented language :)
> > > > > May i clear up ?:
> > > > no :p
> > > > > 1. The classes Customer and Person inherits already from another
> > class.
> > > > > Multiple inheritance is not supported by VB.Net.
> > > > > 2. I have to access the methods of "CommonMethods" as shared
methods
> > of
> > > > the
> > > > > classes Customer and Person. The shared methods are the same for
> many
> > > > other
> > > > > classes. I use this trick with the shared property (which will be
> > > > > initialized by first time using) in each class, because it's not
> > > possible
> > > > to
> > > > > inherit shared methods from a baseclass, but is much less code as
> > > writing
> > > > > the shared methods for each class again and again.
> > > > > Walter
> > > > > > Try something like this:
> > > > > > Dim FInfo As FieldInfo
> > > > > > Dim Value As Object
> > > > > > FInfo = GetType(Customer).GetField("CommonClassMethods",
> > > > > BindingFlags.Static
> > > > > > Or BindingFlags.Public)
> > > > > > Value = FInfo.GetValue(Nothing)
> > > > > > Debug.WriteLine(Value.GetType.GetMethod("MethodA").Invoke(Value,
> > > > > > Nothing).ToString)
> > > > > > or you may also try this
> > > > > > Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> > > > > > System.EventArgs) Handles MyBase.Load
> > > > > > Dim C As New Customer()
> > > > > > Debug.WriteLine(C.MethodA)
> > > > > > End Sub
> > > > > > Class CommonMethods
> > > > > > Protected m_ClassName As String
> > > > > > Public Function MethodA() As String
> > > > > > Return m_ClassName
> > > > > > End Function
> > > > > > End Class
> > > > > > Class Customer : Inherits CommonMethods
> > > > > > Sub New()
> > > > > > m_ClassName = "Customer"
> > > > > > End Sub
> > > > > > End Class
> > > > > > Class Person : Inherits CommonMethods
> > > > > > Sub New()
> > > > > > m_ClassName = "Customer"
> > > > > > End Sub
> > > > > > End Class
> > > > > > This is called Object Oriented language :)
> > > > > > > Sorry for wrong explanation.
> > > > > > > Class CommonMethods
> > > > > > > Private m_ClassName As String
> > > > > > > Sub New (ClassName As String)
> > > > > > > m_ClassName = ClassName
> > > > > > > End Sub
> > > > > > > Function MethodA As String
> > > > > > > 'This is perhaps a stupid but simple example. Of course i
> > have
> > > > more
> > > > > > > methods that makes use of m_ClassName
> > > > > > > Return m_ClassName
> > > > > > > End Function
> > > > > > > End Class
> > > > > > > Class Customer
> > > > > > > Public Shared ReadOnly CommonClassMethods as New
> > > > > > > CommonMethods("Customer")
> > > > > > > End Class
> > > > > > > Class Person
> > > > > > > Public Shared ReadOnly CommonClassMethods as New
> > > > > > CommonMethods("Person")
> > > > > > > End Class
> > > > > > > Now i have two CommonMethods objects which i can use over the
> > shared
> > > > > > > properties of Customer or Person.
> > > > > > > What i want to do, is to call the Method MethodA over the
> > Reflection
> > > > of
> > > > > > the
> > > > > > > shared Properties
> > > > > > > Dim VBType as Type
> > > > > > > VBType = GetType(Customer)
> Console.WriteLine(VBType.GetMethod("CommonClassMethods.MethodA").Invoke)
> > > > > '
> > > > > > > Returns "Customer"
> > > > > > > VBType = GetType(Person)
> Console.WriteLine(VBType.GetMethod("CommonClassMethods.MethodA").Invoke)
> > > > > '
> > > > > > > Returns "Person"
> > > > > > > > DynType.GetMethod("MethodA", BindingFlags.Static)
> > > > > > > > > Hi,
> > > > > > > > > if i have the following Call:
> > > > > > > > > DoSomething(MyObject.GetType)
> > > > > > > > > and the following Function
> > > > > > > > > Sub DoSomething(DynType As Type)
> > > > > > > > > 'I can call any method of this type by
> > > > > > > > > Dim o As Object =
> System.Activator.CreateInstance(DynType)
> > > > > > > > > DynType.GetMethode("MethodA").Invoke(o, Nothing)
> > > > > > > > > End Sub
> > > > > > > > > But how can i call a Method of a shared Property?
> > > > > > > > > Examplecode (does not work):
> > > > > > > > > Sub DoSomething(DynType As Type)
> > > > > > > > > Dim o As Object
> > > > > > > > > o = DynType.GetMethod("SharedProp.MethodA").Invoke
> > > > > > > > > End Sub
> > > > > > > > > Walter