Unable to cast to extended class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If I extend the WebBrowser class like this:

public class CustomWebBrowser : System.Windows.Forms.WebBrowser
{
public string myProperty()
{
return "myProperty";
}
}

and get the ControlsCollection of a Form like this:

ControlsCollection formControls = MyForm.Controls;

and iterate thru the collection like this:

foreach (Control ctl in formControls)
{
// if I am at the CustomWebBrowser control I am unable to cast like this
CustomWebBrowser browser = ctl as CustomWebBrowser;
}

In the above code the browser object is null when doing this with my
CustomWebBrowser class. However, if I just use the WebBrowser class where it
is not extended the control 'ctl' I am able to cast to a WebBrowser class.

How can I cast from the Control class to my CustomWebBrowser class? Any
ideas?

Thank you!
 
Matt Love said:
If I extend the WebBrowser class like this: ....
and get the ControlsCollection of a Form like this:

ControlsCollection formControls = MyForm.Controls;

and iterate thru the collection like this:

foreach (Control ctl in formControls)
{
// if I am at the CustomWebBrowser control I am unable to cast like
this
CustomWebBrowser browser = ctl as CustomWebBrowser;
}

In the above code the browser object is null when doing this with my
CustomWebBrowser class. However, if I just use the WebBrowser class where
it
is not extended the control 'ctl' I am able to cast to a WebBrowser class.

I have tested it and I have got positive result (CustomWebBrowser control
was properly discovered). My modified code:

private void button2_Click(object sender, EventArgs e)
{
foreach (Control ctl in this.Controls)
{
CustomWebBrowser browser = ctl as CustomWebBrowser;

if (browser != null)
MessageBox.Show(browser.myProperty());
}
}
 
Thanks Grzegorz,

I should have mentioned that the CustomeWebBrowser class is defined in
another project and imported into the one I am having problems casting. I am
building an automated test tool where one app launches another.

In the test tool app I am launching my app (which implements the
CustomWebBrowser control) like this:

[STAThread]
public static object StartAUT(string applicationPath, string typeName)
{
Assembly asm = Assembly.LoadFrom(applicationPath);
Type typeUT = asm.GetType(typeName);
object obj = Activator.CreateInstance(typeUT);

Type[] types = new Type[0];
MethodInfo mi = typeUT.GetMethod("Show", types);
mi.Invoke(obj, null);
return obj;
}

and then I am calling the StartAUT method like this:

AUT = (Form)GUITestUtil.StartAUT(AUTPath, startupType);

....this is where I then run thru the foreach (Control ctl in AUT.Controls)

Perhaps this detail is the issue?
 
Hi Matt,

This is probably because the copy of the assembly contains the
CustomWebBrowser that you referenced in your test app is not the same one
as you're loading from. In other word, if you load two copies of the same
assembly from different location, the same class in two copies are NOT the
same type. Here's a simple test to verify the behavior:

1) Create a solution, add a Class Library which contains the custom
WebBrowser. It also contains a WinFom to use the custom WebBrowser.
2) Add a Windows Application, reference the Class Library, note the
ClassLibrary1.dll will be copied to this Windows Application's output
directory.
3) Use the output directory of Class Library to load the ClassLibrary1.dll,
however, since you're referencing the copy in output directory of Windows
Application, they will not be equal:

private void button1_Click(object sender, EventArgs e)
{
string path1 = @"c:\temp\ClassLibrary1\bin\Debug\ClassLibrary1.dll";
string path2 =
@"c:\temp\WindowsApplication1\bin\Debug\ClassLibrary1.dll";
Form aut = (Form)StartAUT(path1, "ClassLibrary1.Form1");
foreach(Control ctl in aut.Controls)
{
ClassLibrary1.Class1 c1 = ctl as ClassLibrary1.Class1;
Debug.Assert(c1 != null);
}
}


If you use path2 above to test, the code will run successfully.

Hope this helps.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

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.
 
Back
Top