开发者

Making a DLL to access to an external class object

I am aware that the title is not self-explanatory but this is the simplest one that I came up with.

Basically, I am have a .NET DLL and a .NET forms application. 开发者_StackOverflowIn this form, I load this DLL and create a class let's say:

MyClass a = new MyClass();

I have:

public string DataBridge
{
 get { return String.Empty; }
 set { txtHistory.Text += value + "\r\n"; }
}

in my form and I want to access this DateBridge string within MyClass which is located inside my .NET DLL.

I am not sure if it's clear, let me know if there is a point that you didn't understand.


MyClass will need to have a reference of your form.


Your .NET Forms application must have a reference to the .NET dll. If you're programming with Visual Studio, you can simply right-click on the project in the Solution Explorer, then choose "Add Reference", and find the DLL that contains MyClass.

Since you can have multiple projects open in one Solution, you can choose to add a reference to your MyClass project rather than the DLL file, this will make sure that changes in your MyClass automatically will be visible in your .NET Forms project.

If you don't use Visual Studio, but some other tool (eg. SharpDevelop), please see that tool's documentation to find out how to add a reference tho another project or dll.

If you don't use a tool at all, but use the CSC.exe compiler to compile you code, see the compiler's documentation to find out how to include a reference to an external dll at compile time.


MyClass can not know about your Form (or your Form does not know about MyClass) as this would create a circular dependency. You need to pass something that both parties knows about. An interface or a delegate could be suitable in this case. Unfortunately c# does can not create delegates to properties.

First I want to note some things about you DataBridge.

  1. get { return String.Empty; } if you don't need to use the result you can skip the getter and only use the setter.
  2. set { txtHistory.Text += value + "\r\n"; }, this as a quite bad design, setters should not have this kind of side effects. If you assign a setter the same value multiple times you expect it to produce the same result as if you just assigned it once.
    Your DataBridge should rather be a method (like DataBridgeAppend(string s)).

If you instead create a method

public string DataBridgeAppend(string s)
{
    txtHistory.Text += s + Environment.Newline;
    // or even better  txtHistory.AppendText(s + Environment.Newline);
}

You can change your class to this

public class MyClass
{
    private Action<string> _dataBridgeAppend;
    public MyClass(Action<string> dataBridgeAppend)
    {
        _dataBridgeAppend = dataBridgeAppend;
    }

    public void DoStuff()
    {
        // stuff
        _dataBridgeAppend("result"); // using the callback delegate to return stuff to the Form
    }
}

from Form1 you instantiate MyClass like this

MyClass a = new MyClass(this.DataBridgeAppend);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜