473,563 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Yet another Reflection / Interface Question

I know this has to be answered in the archives somewhere, but the
search results are just confusing me more :-(

Anyways, I authored a control library that contains an Interface
definition and then some usercontrols that implement that interface.
In a separate project I want to load the dll and then load the
usercontrols onto tabpages on my form.

I just cannot seem to figure out how to cast the usercontrols into the
interface type so I can access the defined methods.

Here's the interface thats defined only in the dll:

public interface IReadonlyTab
{
void CreateBindings( );
String CaptionText { get;}
String ConnectionStrin g { get;set;}
event RequestTitleCha ngeEventHandler RequestTitleCha nge;
}

And here's the nonworking code that loads the controls:

String path = @"C:\Documen ts and Settings\srowe\ Desktop
\MyTabs.dll";
Assembly assembly = Assembly.LoadFr om(path);
Type[] types = assembly.GetTyp es();
foreach (Type t in types)
{
Type I = t.GetInterface( "MainTabs.IRead onlyTab");
if (I != null)
{
//I want to do something similar to this:
Type.GetType(I) c =
(Type.GetType(I ))Activator.Cre ateInstance(t);
TabPage tp = new TabPage(c.Capti onText); //CaptionText is
an Interface defined property
tp.Controls.Add (c);
this.tabControl 1.TabPages.Add( tp);
}
}

In case you haven't realized I'm new to using reflection, so please be
nice if I'm missing something obvious here.

I really appreciate any advice you experts can give!

Thanks,

Seth Rowe

Feb 16 '07 #1
8 3523
rowe_newsgroups <ro********@yah oo.comwrote:
I know this has to be answered in the archives somewhere, but the
search results are just confusing me more :-(

Anyways, I authored a control library that contains an Interface
definition and then some usercontrols that implement that interface.
In a separate project I want to load the dll and then load the
usercontrols onto tabpages on my form.

I just cannot seem to figure out how to cast the usercontrols into the
interface type so I can access the defined methods.
Just cast it to IReadonlyTab - that's the interface you want to use, so
that's what you cast it to.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 16 '07 #2
On Feb 16, 1:35 pm, Jon Skeet [C# MVP] <s...@pobox.com wrote:
rowe_newsgroups <rowe_em...@yah oo.comwrote:
I know this has to be answered in the archives somewhere, but the
search results are just confusing me more :-(
Anyways, I authored a control library that contains an Interface
definition and then some usercontrols that implement that interface.
In a separate project I want to load the dll and then load the
usercontrols onto tabpages on my form.
I just cannot seem to figure out how to cast the usercontrols into the
interface type so I can access the defined methods.

Just cast it to IReadonlyTab - that's the interface you want to use, so
that's what you cast it to.

--
Jon Skeet - <s...@pobox.com >http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Thanks for replying Jon you're a great source of knowledge in this
newsgroup.

I'm afraid I'm still a bit confused as to how to cast it to
IReadonlyTab. The interface is only defined in the dll, not in the
calling application, so I can't do a IReadOnlyTab c =
(IReadOnlyTab)A ctivator.Create Instance(t). That's where I am so
confused, how do I "get" the interface so that I can cast to it?

Thanks again,

Seth Rowe

Feb 16 '07 #3
>
I'm afraid I'm still a bit confused as to how to cast it to
IReadonlyTab. The interface is only defined in the dll, not in the
calling application, so I can't do a IReadOnlyTab c =
(IReadOnlyTab)A ctivator.Create Instance(t). That's where I am so
confused, how do I "get" the interface so that I can cast to it?
If the interface is only defined in the dll and you are not compiling the
dll with the project there is no way you can use it. Usually you compile
your exec. with the interface that all of your controls implement. Take a
look at Jon Skeet's plugin article

If you do not do this you will have to call the methds using reflection,
which is a pain.
--
Ignacio Machin
machin AT laceupsolutions com
Feb 16 '07 #4


Here is some example code:
public static IRateQuoter GetARateQuoter( )
{
//The RateQuoterSetti ngs encapulates the information found in the
App.Config file
//The "Handler" reads the xml ... to create a RateQuoterSetti ngs
instance.
RateQuoterSetti ngs settings =
((RateQuoterSet tings)System.Co nfiguration.Con figurationSetti ngs.GetConfig(C O
NFIG_SECTION_NA ME));
//Now you have a RateQuoterSetti ngs instance.
//use the assembly and class name from the RateQuoterSetti ngs instance.
to dynamically create the object
return CreateInstance( settings.Assemb lyName, settings.ClassN ame ,
settings.Shippi ngCompanyHomeSt ate );

}

private static IRateQuoter CreateInstance( string assemblyName, string
className , string homeState)
{
IRateQuoter returnObject = null;
Assembly assem = Assembly.Load( assemblyName );
if(null != assem)
{
Type objectType = assem.GetType( className , true , true );
//The use of "homeState" is here... to show how the CreateInstance can
have non default constructors.
//Notice the second argument is an array of objects.. the array of
objects ... needs to match one of the contructor-method-signatures
returnObject = (IRateQuoter)Ac tivator.CreateI nstance( objectType , new
object[] {homeState} );

}
return returnObject;

}
Full code download is available at:

http://sholliday.spaces.live.com/blog/

12/1/2005
Understanding the Simple Factory Pattern


"rowe_newsgroup s" <ro********@yah oo.comwrote in message
news:11******** *************@q 2g2000cwa.googl egroups.com...
I know this has to be answered in the archives somewhere, but the
search results are just confusing me more :-(

Anyways, I authored a control library that contains an Interface
definition and then some usercontrols that implement that interface.
In a separate project I want to load the dll and then load the
usercontrols onto tabpages on my form.

I just cannot seem to figure out how to cast the usercontrols into the
interface type so I can access the defined methods.

Here's the interface thats defined only in the dll:

public interface IReadonlyTab
{
void CreateBindings( );
String CaptionText { get;}
String ConnectionStrin g { get;set;}
event RequestTitleCha ngeEventHandler RequestTitleCha nge;
}

And here's the nonworking code that loads the controls:

String path = @"C:\Documen ts and Settings\srowe\ Desktop
\MyTabs.dll";
Assembly assembly = Assembly.LoadFr om(path);
Type[] types = assembly.GetTyp es();
foreach (Type t in types)
{
Type I = t.GetInterface( "MainTabs.IRead onlyTab");
if (I != null)
{
//I want to do something similar to this:
Type.GetType(I) c =
(Type.GetType(I ))Activator.Cre ateInstance(t);
TabPage tp = new TabPage(c.Capti onText); //CaptionText is
an Interface defined property
tp.Controls.Add (c);
this.tabControl 1.TabPages.Add( tp);
}
}

In case you haven't realized I'm new to using reflection, so please be
nice if I'm missing something obvious here.

I really appreciate any advice you experts can give!

Thanks,

Seth Rowe

Feb 16 '07 #5
On Feb 16, 2:17 pm, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions .comwrote:
I'm afraid I'm still a bit confused as to how to cast it to
IReadonlyTab. The interface is only defined in the dll, not in the
calling application, so I can't do a IReadOnlyTab c =
(IReadOnlyTab)A ctivator.Create Instance(t). That's where I am so
confused, how do I "get" the interface so that I can cast to it?

If the interface is only defined in the dll and you are not compiling the
dll with the project there is no way you can use it. Usually you compile
your exec. with the interface that all of your controls implement. Take a
look at Jon Skeet's plugin article

If you do not do this you will have to call the methds using reflection,
which is a pain.

--
Ignacio Machin
machin AT laceupsolutions com
Thanks for the response!

Unfortunately, you confirmed my fears - I was just hoping in my
"newness" to reflection I was missing something. I guess I could use
reflection to just call the methods as you suggest, but how do I map
the events and get/set the properties? Sorry, this is really my first
exposure to reflection. Any good links for this?

Thanks,

Seth Rowe

Feb 16 '07 #6
On Feb 16, 2:17 pm, "sloan" <s...@ipass.net wrote:
Here is some example code:
public static IRateQuoter GetARateQuoter( )
{
//The RateQuoterSetti ngs encapulates the information found in the
App.Config file
//The "Handler" reads the xml ... to create a RateQuoterSetti ngs
instance.
RateQuoterSetti ngs settings =
((RateQuoterSet tings)System.Co nfiguration.Con figurationSetti ngs.GetConfig(C O
NFIG_SECTION_NA ME));
//Now you have a RateQuoterSetti ngs instance.
//use the assembly and class name from the RateQuoterSetti ngs instance.
to dynamically create the object
return CreateInstance( settings.Assemb lyName, settings.ClassN ame ,
settings.Shippi ngCompanyHomeSt ate );

}

private static IRateQuoter CreateInstance( string assemblyName, string
className , string homeState)
{
IRateQuoter returnObject = null;
Assembly assem = Assembly.Load( assemblyName );
if(null != assem)
{
Type objectType = assem.GetType( className , true , true );
//The use of "homeState" is here... to show how the CreateInstance can
have non default constructors.
//Notice the second argument is an array of objects.. the array of
objects ... needs to match one of the contructor-method-signatures
returnObject = (IRateQuoter)Ac tivator.CreateI nstance( objectType , new
object[] {homeState} );

}
return returnObject;

}

Full code download is available at:

http://sholliday.spaces.live.com/blog/

12/1/2005
Understanding the Simple Factory Pattern

"rowe_newsgroup s" <rowe_em...@yah oo.comwrote in message

news:11******** *************@q 2g2000cwa.googl egroups.com...
I know this has to be answered in the archives somewhere, but the
search results are just confusing me more :-(
Anyways, I authored a control library that contains an Interface
definition and then some usercontrols that implement that interface.
In a separate project I want to load the dll and then load the
usercontrols onto tabpages on my form.
I just cannot seem to figure out how to cast the usercontrols into the
interface type so I can access the defined methods.
Here's the interface thats defined only in the dll:
public interface IReadonlyTab
{
void CreateBindings( );
String CaptionText { get;}
String ConnectionStrin g { get;set;}
event RequestTitleCha ngeEventHandler RequestTitleCha nge;
}
And here's the nonworking code that loads the controls:
String path = @"C:\Documen ts and Settings\srowe\ Desktop
\MyTabs.dll";
Assembly assembly = Assembly.LoadFr om(path);
Type[] types = assembly.GetTyp es();
foreach (Type t in types)
{
Type I = t.GetInterface( "MainTabs.IRead onlyTab");
if (I != null)
{
//I want to do something similar to this:
Type.GetType(I) c =
(Type.GetType(I ))Activator.Cre ateInstance(t);
TabPage tp = new TabPage(c.Capti onText); //CaptionText is
an Interface defined property
tp.Controls.Add (c);
this.tabControl 1.TabPages.Add( tp);
}
}
In case you haven't realized I'm new to using reflection, so please be
nice if I'm missing something obvious here.
I really appreciate any advice you experts can give!
Thanks,
Seth Rowe
Thanks for the reply sloan, but I'm afraid I'm not sure how to use
what you are showing me? All I really need is to access the
usercontrol's methods, events, and properties that are defined by an
interface in it's dll.

Could you please elaborate a bit more?

Thanks,

Seth Rowe

Feb 16 '07 #7
rowe_newsgroups <ro********@yah oo.comwrote:
Unfortunately, you confirmed my fears - I was just hoping in my
"newness" to reflection I was missing something. I guess I could use
reflection to just call the methods as you suggest, but how do I map
the events and get/set the properties? Sorry, this is really my first
exposure to reflection. Any good links for this?
Events and properties are available in reflection in the same way as
methods - Type.GetPropert y and Type.GetEvent. Look at PropertyInfo and
EventInfo for what you can then do with them.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 16 '07 #8
On Feb 16, 3:49 pm, Jon Skeet [C# MVP] <s...@pobox.com wrote:
rowe_newsgroups <rowe_em...@yah oo.comwrote:
Unfortunately, you confirmed my fears - I was just hoping in my
"newness" to reflection I was missing something. I guess I could use
reflection to just call the methods as you suggest, but how do I map
the events and get/set the properties? Sorry, this is really my first
exposure to reflection. Any good links for this?

Events and properties are available in reflection in the same way as
methods - Type.GetPropert y and Type.GetEvent. Look at PropertyInfo and
EventInfo for what you can then do with them.

--
Jon Skeet - <s...@pobox.com >http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Thanks! This is going to be a bit more work than I hoped (isn't always
that way?), but this should get me to where I need to be!

Thanks again to everyone,

Seth Rowe

Feb 16 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
7347
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the...
2
6363
by: cv | last post by:
Hi, I want to create an object from a class I load from an assembly. Then I want to cast that object to a class (an interface) 'known' to the program in order to pass it to other methods that work with that interface. The casting is invalid but I can't see why, as the class I load from the assembly really is of the type I cast it to. ...
12
2351
by: Antony | last post by:
Hello - I am wanting to print out a "Yes" next to classes that implement "Interface01", otherwise a "No". Here is my code. It crashes with a null reference exception and I am not sure why. Ideas? Thank you for you help. Tony Imports System.Reflection Public Class Form1
3
2321
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided through an external file in which case we can use reflection to create objects of that type at run time. Case 1: The methods of those types can...
4
1492
by: Doug Handler | last post by:
Hi, I've developed a plug-in application that basically is constructed of 2 pieces 1). the "main framework" 2). channels. Using Reflection in the Main Framework, i dynamically load Channels. This works fine and great. My problem is w/ menus. Each Channel returns a Popup Menu (based on an interface requirement) that gets placed in the...
5
1849
by: | last post by:
I am having problems with casting or converting a class to to an interface from which it derives. I'm certain that it's due to how it's being loaded, but I'm not sure how to get past the problem. Here's a general outline of the architecture. It's oversimplified, but I think it's enough info to help: Assembly A.dll { interface IMyBase...
7
2668
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file name based on the name of the VB6 application. A second choice would be a file name based on the # COM interface assembly. I have tried calling...
17
2295
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection to build a compiler? One that will construct a user defined class "on the fly" (literally, the user defines a class, instantiates it, and runs it...
0
7580
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7882
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7634
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5481
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3634
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.