简体   繁体   中英

C# - passing class reference to a C# DLL, and then accessing it

I'm wondering if I am allowed to pass value reference of class that loaded the DLL to the DLL and then access the class through that value saved. I have not found any sources ever mentioning or any examples showing such. I do imagine you would traditionally rather call upon a function(s) inside the DLL, and return values, but I am working on a game server and want to have DLL's have access to document classes, especially the main program class, and directly modify numbers needed. For as3 I can do this example down below.

class = new _Class(MovieClip(this)); //passes current class value `ref`

And then inside _Class , you save it like such

           public var saveValueRefOfClass:MovieClip;

            public function Entity(val:MovieClip)
            {
                this.saveValueRefOfClass = val; //save it
            }

and then access the value ref inside the _Class like so;

    saveValueRefOfClass.anyFunc(); //call back to that class

Besides strongly recommending against this approach in comments, you can do this by simply including the main .exe dependency to your DLL project.

In the solution explorer, just right-click in the DLL project -> Add -> Reference and select the executable project name in the tab Projects .

This will expose all public names declared in the executable project to your DLL project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM