Reflection

  • Thread starter Thread starter Joe
  • Start date Start date
J

Joe

Hi all!

I have an application which uses plug-ins. The plug-ins implement an
interface. I have a case where 1 plug-in needs access to the an object of
the calling application. I could create an event in the interface to allow
for this but this will break all the other plug-ins using the interface.
(Would be nice if I could add an Optional attribute or something).

I was thinking of using reflection in the plug-in to call a method in the
application but I'm not too sure how to access the object.


Any suggestions?

Thanks,
Joe
 
public interface IPlugin
{
void Blah();
}

public interface IPlugin2 : IPlugin
{
void SomethingElse();
}



In the app

IPlugin2 plugin2 = plugin as IPlugin2;
if (plugin2 != null)
plugin2.SomethingElse();



Pete
 
IPlugin2 doesn't need to descend from IPlugin, in fact there is no advantage
in doing so that I can see.
 
Peter,

My Plug-ins work fine. The issue is not how to use an interface but whether
or not an interface method or event can be optional (which I don't think is
possible) or how can I execute a method that is part of an object in the exe
from the Plug-in.
 
Well, what you are saying is that you want to have an interface to something
without extending the interface and breaking other classes. Peter is
right...just add a second interface to the new object and implement them both
in the new class. Then you just cast from one interface to another to get the
ability to access the event from the app. If you don't know whether an
object implements interface #2 just put it in a try/catch when casting (or
using any of the typecasting capabilities of c#) to make the code as generic
as possible.
 
I thought about the 2nd interface but didn't really want to go that route.
If I can get the reflection to work in the plug-in then I'll use that. If
all else fails I'll have to add the second interface.
 
So, you don't want to do it the faster + cleaner + proper way, but would
consider a complete hack instead?

Add the interface!
 
I'm probably going to add the second interface. I didn't want to because the
plug-in that needs it is a 1-off and I didn't want to make an specific
changes for it other then in the plug-in itself.
 
Do you know in advance what the object you need to access would be?

If that is the case, I guess the simplest solution would be to add a
static property on the object you need to access that refers to
itself.

public class AccessMe
{
public static AccessMe AccessMeRef;

public AccessMe()
{
AccessMeRef = this;
}
}

Then on the interface call you will be able to get a reference to the
object using the static property.

public void MyInteraceMethod()
{
AccessMe.AccessMeRef.DoWhatever();
}

Is this an alternative?

Rene.
 
You could do it in a generic way

public interface IPluginProperties
{
bool HasProperty(string name);
object GetProperty(string name);
}
 
Hello Joe,

I am not sure if I understand your question correctly. Did you mean, given
an interface of the host application, how can we (the plugin module) invoke
the host's method with .NET reflection? Suppose that your plugin
architecture is like
http://www.codeproject.com/KB/cs/c__plugin_architecture.aspx
where the IPluginHost value points to the host form (Form1), we can invoke
the functions in Form1 in this way:

Type t = Host.GetType();
t.InvokeMember("F1", BindingFlags.InvokeMethod, null, Host,
null);
t.InvokeMember("F2", BindingFlags.InvokeMethod, null, Host,
null);

class Form1 : System.Windows.Forms.Form, IPluginHost
{
#region IPluginHost Members
public void F1()
{
Console.WriteLine("F1");
}
#endregion

public void F2()
{
Console.WriteLine("F2");
}
}

Am I understanding the issue correctly? Please let me know whether it helps
or not.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello Joe,

I am writing to check the status of the issue on your side. Would you mind
letting me know the result of the suggestions? If you need further
assistance, feel free to let me know. I will be more than happy to be of
assistance.

Have a great day!

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
Back
Top