|
-
Apr 28th, 2009, 10:40 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Automating Host through an AddIn
Through this blog I've learned how to extend my application. I can call methods in my Add-ins but how can I call methods in my Host Application through the Add-in?
Thanks.
-
Apr 28th, 2009, 11:03 PM
#2
Re: Automating Host through an AddIn
The application would have to pass a reference to some object that exposed the desired methods into the plug-in. That object would implement an interface that is defined in the same location as your plug-in interface.
A better option would be for the plug-in to raise an event that the host can then handle and act on. These events would be declared in your plug-in interface.
-
Apr 29th, 2009, 12:12 AM
#3
Thread Starter
Fanatic Member
Re: Automating Host through an AddIn
 Originally Posted by jmcilhinney
The application would have to pass a reference to some object that exposed the desired methods into the plug-in. That object would implement an interface that is defined in the same location as your plug-in interface.
That is the first thing come accross in my mind but the problem is I need to create a new instance of that class in my add-ins and I won't be able to access certain variable that my host application loaded.
 Originally Posted by jmcilhinney
A better option would be for the plug-in to raise an event that the host can then handle and act on. These events would be declared in your plug-in interface.
I think, what I need is my add-in must have direct access to the host application's methods.
Now I am trying to reverse the operation but I think I'm already stuck.
I hope there are tutorials or samples available on how to do it.
-
Apr 29th, 2009, 12:35 AM
#4
Re: Automating Host through an AddIn
 Originally Posted by eSPiYa
That is the first thing come accross in my mind but the problem is I need to create a new instance of that class in my add-ins and I won't be able to access certain variable that my host application loaded.
Of course you don't. If you did it would be useless. I specifically said that the application must pass the object to the plug-in, which it would do by setting a property or calling a method and passing an argument, which are the very same ways you get any data into any object.
 Originally Posted by eSPiYa
I think, what I need is my add-in must have direct access to the host application's methods.
Now I am trying to reverse the operation but I think I'm already stuck.
I hope there are tutorials or samples available on how to do it.
Um, how exactly can the plug-in have direct access to the application without the application passing the plug-in a reference to an object of some sort? That was my the first suggestion.
-
Apr 29th, 2009, 01:03 AM
#5
Thread Starter
Fanatic Member
Re: Automating Host through an AddIn
Thanks.
But can you give me a sample on how to do it?
I'm still new in this.
-
Apr 29th, 2009, 01:21 AM
#6
Re: Automating Host through an AddIn
Define a property in your plug-in interface, implement it in your plug-in classes and set it in your application. That's it. I would assume that you're pretty much doing that already, so all you have to decide is exactly what type this object will be and how to implement it.
-
Apr 29th, 2009, 01:28 AM
#7
Thread Starter
Fanatic Member
Re: Automating Host through an AddIn
Thanks.
I've already created a test Interface in the contract part.
Maybe I can reverse the process of calling methods.
I'll update you later.
-
Apr 29th, 2009, 01:37 AM
#8
Re: Automating Host through an AddIn
 Originally Posted by eSPiYa
Thanks.
I've already created a test Interface in the contract part.
Maybe I can reverse the process of calling methods.
I'll update you later.
As I said, I don't think that that's the best idea. Is there a specific reason that you can't just raise an event. That's going to invoke a method in the application as surely as calling it directly will because the application will attach the appropriate event handlers when it loads a plug-in. It also means that your plug-ins can remain blissfully unaware of the application that they're part of, as they should do.
-
Apr 29th, 2009, 01:59 AM
#9
Thread Starter
Fanatic Member
Re: Automating Host through an AddIn
I'm sorry but I thought I need to create that property on the Contract part.
Anyway, the aim of this project is I need to build a plug-in that hosts a socket-based service. Whenever it receives data it will pass those data to the Host application through calling a method that resides on the Host.
Thanks.
-
Apr 29th, 2009, 02:12 AM
#10
Re: Automating Host through an AddIn
If you want the plug-in to be able to call a method of "the application" directly then the plug-in has to be aware of the type that represents the application. That would mean defining another interface to represent the application and then your plug-in interface would have to have a property of that type. The host app would then pass an object of that type to the plug-in so the plug-in can call its methods.
If you go the option I'm recommending then you don't need the extra interface and your plug-in interface simply adds some events and your plug-in raises those events when it needs to notify the application that something has or should happen. The application will then act accordingly. For instance, instead of the app exposing a Beep method that the plug-in would call, the plug-in would raise a Beep event,, which the app would handle and then make a Beep sound.
It's exactly the same as having a Button on your form to perform some action. When the user clicks the Button it doesn't call a method of your form does it? No, it doesn't. It simply raises its Click event. It's up to the form to handle that event and then act accordingly.
You should also note that, if you need to pass data from the plug-in to the app, you can do so through the event's arguments.
I would suggest that you do a bit of reading on defining and raising events.
-
Apr 29th, 2009, 02:41 AM
#11
Thread Starter
Fanatic Member
Re: Automating Host through an AddIn
I'm sorry if I got confused.
Passing reference of an object through pipeline from host to the add-in still needs a contract because it would become a complex data object. The only supported types that can be transfer between the host application and add-in are basic data-types.
-
Apr 29th, 2009, 02:51 AM
#12
Re: Automating Host through an AddIn
That's exactly what I said. You would have to define an additional interface and then declare a property of that type in the plug-in interface. The application would instantiate a class that implements that interface and then set the property of the plug-in.
I should point out that I haven't actually read that blog. I wasn't previously aware that VS provided such functionality and was speaking in the general sense.
-
Apr 29th, 2009, 02:52 AM
#13
Re: Automating Host through an AddIn
That's exactly what I said. You would have to define an additional interface and then declare a property of that type in the plug-in interface. The application would instantiate a class that implements that interface and then set the property of the plug-in.
I should point out that I haven't actually read that blog. I wasn't previously aware that VS provided such functionality and was speaking in the general sense. I might have to read that and see how it works specifically.
-
Apr 29th, 2009, 03:08 AM
#14
Thread Starter
Fanatic Member
Re: Automating Host through an AddIn
Thanks.
I'll post link to another blog that teaches how to pass complex data between Host Application and Add-in.
After trying your suggestion, I'll post if it will become successful but I've read somewhere that referencing an object across the pipeline might cause problem. It is something about the lifetime of a token. I'm not sure about it.
-
May 2nd, 2009, 09:40 PM
#15
Re: Automating Host through an AddIn
I've been through that blog and implemented the code as described. I haven't tried to adapt that code myself but I think the key to what you want to do can be found in the Host Side Adapter and Add In Adapter sections. Near the top of the Host Side Adapter section it says:
In this sample, the host calls methods on the add-in, and not the opposite. Therefore, we only need an adapter from the Contract to the Host View.
while near the top of the Add In Adapter section it says:
In this sample, the host calls methods on the add-in, and not the opposite. Therefore, we only need an adapter from the Add-In View to the contract.
That suggests to me that, in order for the add-in to call methods on the host, you would need to implement a host side adapter from the Host View to the Contract and another from the Contract to the Add-In View.
-
May 3rd, 2009, 07:21 PM
#16
Thread Starter
Fanatic Member
Re: Automating Host through an AddIn
I would try that too.
Thanks.
-
May 4th, 2009, 02:44 AM
#17
Thread Starter
Fanatic Member
Re: Automating Host through an AddIn
I have both adapters but I don't know how to make a constructor of an abstract class in the Add-in side.
This is my test Class in the add-in side Code:
public abstract class HostClassView { public abstract AuthData Authenticate(string UserName, string Password); }
I need to make a new instance of this class in-order to call its method(Authenticate).
The implementation of the method in the host-side will just look for shared/internal lists in other class so it is fine to create the new instance in the add-in side.
-
May 4th, 2009, 02:54 AM
#18
Re: Automating Host through an AddIn
You can't instantiate an abstract class. An abstract class is like an interface except that, instead of just defining the set of functionality that implementing classes will have, an abstract class actually implements some or all of that functionality. Still, in order for an instance to be created the abstract class must be inherited by a non-abstract class.
-
May 4th, 2009, 03:07 AM
#19
Thread Starter
Fanatic Member
Re: Automating Host through an AddIn
Is it possible to create a new class base on the abstract class without overriding its methods?
I think this should be taken in the consideration because it was already overridden in the adapter class.
And the constructor method might become empty.
-
May 4th, 2009, 04:39 AM
#20
Re: Automating Host through an AddIn
When inheriting an abstract class you MUST override every abstract member or else your derived class must also be declared abstract, which wouldn't help you at all. Any members that are already implemented can be overridden or not as is appropriate for each one.
-
May 4th, 2009, 05:06 AM
#21
Thread Starter
Fanatic Member
Re: Automating Host through an AddIn
So, reversing the operation would be too complicated as of now.
Passing reference is complicated if passed through the pipeline but working though I'm experiencing some problem with complex data.
-
May 4th, 2009, 08:44 AM
#22
Thread Starter
Fanatic Member
Re: Automating Host through an AddIn
Thanks for the help.
I'm successfully call methods of the host application from my Add-in through passing reference of it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|