tags:

views:

72

answers:

7

I am trying to access the controls' properties on another form without having to modify the code of the other projec (the one containing the controls that I want to access) because it is already compiled as a DLL. In this DLL that I am trying to access, the functions/sub-procedures are all declared as private. Would there be any way of accessing the controls' properties without having to modify the DLL? Basically what I am trying to do is create a sort of console application wrapper for the DLL that would create a new instance of the DLL's form and then make certain checkboxes checked and click certain buttons. Basically, I am trying to automate the form as it currently exists.

+1  A: 

You can not access private properties from anywhere, the way to do this is to modify (which you don't want to do :() class and turn those properties with public/global scope

Sarfraz
@MarkJ: did i talk about reflection, could not get you?
Sarfraz
No you didn't talk about reflection, sorry I meant that comment for another answer. I will delete it.
MarkJ
+2  A: 

Private means "private". You can't access private members of another class.

Not without using Reflection, that is.

John Saunders
Even if you can access the private parts using reflection you shouldn't. You will create an unmaintainable mess.
MarkJ
A: 

You can use Delegate.CreateDelegate to call private methods/properties of another class.

var foo = new Foo();
var doSomething = (Func<String, String>)
    Delegate.CreateDelegate(typeof(Func<String, String>), foo, "DoSomething");
Console.WriteLine(doSomething("Hello!"));
Nenad
A: 

Private values are meant not to be accessed from out side but you can do this using reflection but as others say reflection can create mess,anyways have a look at this and this. Hope it helps.

Vinay Pandey
A: 

If a control DLL is built with private accessors it's probably that way for a reason. But of course, not all programmers design their classes right from the start, and sometimes there might be circumstances where you need to access some private properties, like in your case. If you want to access a field that is private you can do that using reflection, as others have mentioned.

Try this for a field

string theFieldName = "_member";
obj.GetType().GetField(
   theFieldName,
   System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance
).GetValue(obj);

Or this if you are after a method

string theMethodName = "_someMethod";
obj.GetType().GetMethod(
     theMethodName,
     System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance
).Invoke(obj, parameters);

You need to supply the BindingFlags to get a particular instance's private contents.

Patrick
This seems like it may almost work for what I need, but I am trying to get the property value of 'Checked' for a checkbox on another form
DaRkMuCk
I tried something like this, but I get an object reference not set to an instance of object error:Dim xmlGenForm As New XMLGen.FormGenerator xmlGenForm.Show() Dim CheckBoxCopyToAppcluster As String = "CheckBoxCopyToAppcluster.Checked" MsgBox(xmlGenForm.[GetType]().GetField(CheckBoxCopyToAppcluster, System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance).GetValue(xmlGenForm).ToString)
DaRkMuCk
A: 

I figured it out, but for some reason the other form isn't updating the checkbox

    Dim chk As New CheckBox
    chk.Checked = False
    Dim xmlGenForm As New XMLGen.FormGenerator
    xmlGenForm.Show()
    Dim pInfo As System.Reflection.PropertyInfo
    pInfo = xmlGenForm.GetType().GetProperty("CheckBoxCopyToAppcluster", Reflection.BindingFlags.IgnoreCase Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
    pInfo.SetValue(xmlGenForm, chk, Nothing)

    If CBool(pInfo.GetValue(xmlGenForm, Nothing).CheckState) = True Then
        MsgBox("checked")
    Else
        MsgBox("not checked")
    End If
DaRkMuCk
A: 

It turned out to be a lot easier doing it like this:

Dim xmlGenForm As New FormGenerator
xmlGenForm.Show()
Dim xmlGenFormGroupBox2 As GroupBox = xmlGenForm.Controls("GroupBox2")
Dim CheckBoxCopyToAppcluster As CheckBox = xmlGenFormGroupBox2.Controls("CheckBoxCopyToAppcluster")

CheckBoxCopyToAppcluster.CheckState = CheckState.Checked
DaRkMuCk