best practice question

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

Is there a reasonbly good way to determine what controls will be present in a
class inherited from form, with designer-generated code, without intantiating?

Bob
 
If the class is not instantiated then, as there is no instance of the class,
there will be none.
 
Hi Bob,

You can query the Form's Type using Reflection.

Have a play with this:

<code>
Public Sub WhatsInAForm (oFormType As Type)
Dim aF() As FieldInfo
Dim aM() As MemberInfo
Dim Bf As BindingFlags = _
BindingFlags.Instance Or _
BindingFlags.NonPublic Or _
BindingFlags.DeclaredOnly

aF = GetType (oFormType).GetFields (Bf)
Console.WriteLine ("Fields")
For I = 0 To aF.Length - 1
Console.WriteLine ("I = {0} = {1}", I, aF(I).Name)
Next

aM = GetType (oFormType).GetMembers (Bf)
Console.WriteLine ("Members")
For I = 0 To aM.Length - 1
Console.WriteLine ("I = {0} = {1}", I, aM(I).Name)
Next
End Sub
</code>

The BindingFlags are
BF.Instance - Make a FormX to examine.
BF.NonPublic - Controls are Friend, ie. not Public
BF.DeclaredOnly - Skip all the base class stuff.

Regards,
Fergus
 
Hi Bob,

Two corrections.

I added the Form Type parameter as an afterthought.
You'll need
aF = oFormType.GetFields (Bf)
aM = oFormType.GetMembers (Bf)
instead of
aF = GetType (oFormType).GetFields (Bf)
aM = GetType (oFormType).GetMembers (Bf)

More importantly, this method <does> instantiate the Form. It will call
the constructor though it won't call Form_Load.

Regards,
Fergus
 
Now why did I think I couldn't use that to see private members? That works
great, that you. :)

Bob
 
Back
Top