Yet another Reflection / Interface Question

R

rowe_newsgroups

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 ConnectionString { get;set;}
event RequestTitleChangeEventHandler RequestTitleChange;
}

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

String path = @"C:\Documents and Settings\srowe\Desktop
\MyTabs.dll";
Assembly assembly = Assembly.LoadFrom(path);
Type[] types = assembly.GetTypes();
foreach (Type t in types)
{
Type I = t.GetInterface("MainTabs.IReadonlyTab");
if (I != null)
{
//I want to do something similar to this:
Type.GetType(I) c =
(Type.GetType(I))Activator.CreateInstance(t);
TabPage tp = new TabPage(c.CaptionText); //CaptionText is
an Interface defined property
tp.Controls.Add(c);
this.tabControl1.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
 
J

Jon Skeet [C# MVP]

rowe_newsgroups said:
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.
 
R

rowe_newsgroups

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

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)Activator.CreateInstance(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
 
S

sloan

Here is some example code:
public static IRateQuoter GetARateQuoter()
{
//The RateQuoterSettings encapulates the information found in the
App.Config file
//The "Handler" reads the xml ... to create a RateQuoterSettings
instance.
RateQuoterSettings settings =
((RateQuoterSettings)System.Configuration.ConfigurationSettings.GetConfig(CO
NFIG_SECTION_NAME));
//Now you have a RateQuoterSettings instance.
//use the assembly and class name from the RateQuoterSettings instance.
to dynamically create the object
return CreateInstance( settings.AssemblyName, settings.ClassName ,
settings.ShippingCompanyHomeState );

}

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)Activator.CreateInstance( 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_newsgroups said:
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 ConnectionString { get;set;}
event RequestTitleChangeEventHandler RequestTitleChange;
}

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

String path = @"C:\Documents and Settings\srowe\Desktop
\MyTabs.dll";
Assembly assembly = Assembly.LoadFrom(path);
Type[] types = assembly.GetTypes();
foreach (Type t in types)
{
Type I = t.GetInterface("MainTabs.IReadonlyTab");
if (I != null)
{
//I want to do something similar to this:
Type.GetType(I) c =
(Type.GetType(I))Activator.CreateInstance(t);
TabPage tp = new TabPage(c.CaptionText); //CaptionText is
an Interface defined property
tp.Controls.Add(c);
this.tabControl1.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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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)Activator.CreateInstance(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.
 
R

rowe_newsgroups

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.

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
 
R

rowe_newsgroups

Here is some example code:
public static IRateQuoter GetARateQuoter()
{
//The RateQuoterSettings encapulates the information found in the
App.Config file
//The "Handler" reads the xml ... to create a RateQuoterSettings
instance.
RateQuoterSettings settings =
((RateQuoterSettings)System.Configuration.ConfigurationSettings.GetConfig(CO
NFIG_SECTION_NAME));
//Now you have a RateQuoterSettings instance.
//use the assembly and class name from the RateQuoterSettings instance.
to dynamically create the object
return CreateInstance( settings.AssemblyName, settings.ClassName ,
settings.ShippingCompanyHomeState );

}

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)Activator.CreateInstance( 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


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 ConnectionString { get;set;}
event RequestTitleChangeEventHandler RequestTitleChange;
}
And here's the nonworking code that loads the controls:
String path = @"C:\Documents and Settings\srowe\Desktop
\MyTabs.dll";
Assembly assembly = Assembly.LoadFrom(path);
Type[] types = assembly.GetTypes();
foreach (Type t in types)
{
Type I = t.GetInterface("MainTabs.IReadonlyTab");
if (I != null)
{
//I want to do something similar to this:
Type.GetType(I) c =
(Type.GetType(I))Activator.CreateInstance(t);
TabPage tp = new TabPage(c.CaptionText); //CaptionText is
an Interface defined property
tp.Controls.Add(c);
this.tabControl1.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!

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
 
J

Jon Skeet [C# MVP]

rowe_newsgroups said:
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.GetProperty and Type.GetEvent. Look at PropertyInfo and
EventInfo for what you can then do with them.
 
R

rowe_newsgroups

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top