开发者

How to access controls' private properties on another form?

开发者 https://www.devze.com 2022-12-21 18:25 出处:网络
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 comp

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 wrappe开发者_运维问答r 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.


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

Not without using Reflection, that is.


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


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!"));


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.


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.


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消