IsSubclassOf Fails!!!

  • Thread starter Thread starter Mark Olbert
  • Start date Start date
M

Mark Olbert

I am going absolutely insane with this problem.

I've got two types that SHOULD be related.

public class B : A
{
}

B.GetType().IsSubclassOf(typeof(A)) returns FALSE

even though

B.GetType().BaseType.AssemblyQualifiedName == A.GetType().AssemblyQualifiedName

and I mean the two strings are exactly identical, down to the full version, the assembly name and the public key that's signed both
assemblies.

What's causing me to pull my hair out is that this stupid simple little test was working a week ago, and I don't recall doing
anything to the code to cause it to break...which means it must be something so obvious I'm not seeing it, or so subtle that I
wasn't aware that I was risking breaking something that was working.

Does anyone know of any instances where the B.GetType().IsSubclassOf(typeof(A)) will fail even though it shouldn't????

- Mark
 
Does anyone know of any instances where the B.GetType().IsSubclassOf(typeof(A)) will fail even though it shouldn't????

Are the two types in the same assembly? If not, how are the assemblies
loaded?


Mattias
 
B.GetType()?
Is by that "B" you mean an instance of class B?
Is A an interface or a class?
what does typeof(B).IsSubclassOf(typeof(A)) return?

In case you do not figure out, and may consider to use IsAssignableFrom
method instead. It support both class and interface:
typeof(A).IsAssignableFrom(typeof(B))

Also consider typeof(A).IsInstanceOfType(BInstance), or simply use the
"is" operator.
 
Wow, that was a fast response!

Mattias, the two types are defined in separate assemblies:

Assembly One:
public class A ...

Assembly Two:
public class B : A...
From your question it sounds like this could be related to the problem.
If so, I have to confess I would be quite confused.

In any event, does that suggest any further thoughts?

- Mark
 
Thanks for the suggestions, I'll give them a try.

BTW, you're quite right that my post was confusing. When I wrote:

B.GetType().IsSubclassOf(typeof(A)) = false

what I meant, and should've written, was,

InstanceOfB.GetType().IsSubclassOf(typeof(A)) = false

Sorry about the confusion.
 
Mattias,

I've continued to do some more googling, and came across a reference
you provided to someone else about how the same assembly loaded
statically and dynamically are not considered the same assembly...even
if they're based on the exact same file and have the same identity
(i.e., strong name and version).

My application does make use of LoadFrom() to retrieve information
about class B in my example. Class A is defined in an assembly that is
statically referenced in the application. That sounds like it may be
the source of my problem, don't you think?

I must confess that I had no idea until just now that AppDomain
maintained two separate lists of assemblies, static and dynamic. I
always assumed it maintained just one.

Is there a way to test for type identity given my need to use
LoadFrom()? I guess I could just compare AssemblyQualifiedName, but I'm
not sure how that will affect the rest of my application, I'll have to
dig into it. Sigh.

Anyway, thanks for the leads, and please let me know if I've
misunderstood something here.

- Mark
 
Hi mark,

Thanks for your post.

Currently, it seems that I still can not reproduce out your problem. I
writen a little winform sample, code snippet listed below:

private void button1_Click(object sender, System.EventArgs e)
{
Process p=Process.GetCurrentProcess();
string assemblyname=p.ProcessName+".exe";
Assembly a=Assembly.LoadFrom (assemblyname);
Module[] modules=a.GetModules();
Type formtype=typeof(System.Windows.Forms.Form);
StringBuilder sb=new StringBuilder();
foreach(Module m in modules)
{
Type []types=m.GetTypes();
foreach(Type t in types)
{
if(t.IsSubclassOf (formtype))
{
sb.AppendFormat("{0}\n",t.Name );
}
}
}
MessageBox.Show(sb.ToString());
}
Note: the code will get the Form1 class in my winform applicaiton, which
means t.IsSubclassOf (formtype) returns true. Also,
System.Windows.Forms.Form class resides in System.Windows.Forms.dll, which
is not the same dll as my loading assembly.

Can you try the above code on your side? Please feel free to feedback your
test here. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Jeffrey,

I've confirmed that the problem derives from the fact that .NET distinguishes between an assembly loaded statically (i.e., via a
reference in a project) and the exact same assembly loaded dynamically (i.e., via something like Assembly.Load(), or
Assembly.LoadFrom()).

- Mark
 
Hi Mark,

Thanks for your feedback.

Yes, I just want to work with you to confirm this behavior. To confirm what
you believe, I have to reproduce this behavior on my side, so I wrote a
sample for this scenario first.
However, based on my sample code provided in the last reply, it seems that
I got different result.

In the sample, Type formtype=typeof(System.Windows.Forms.Form), gets the
type from the static loaded System.windows.forms.dll assembly, while in
t.IsSubclassOf (formtype) statement, the t comes from another dynamically
loaded assembly. However, this statement results in true in my sample. So
we got different result now.

I need your further feedback information on confirmation your comment.

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Jeffrey,

My apologies for misunderstanding you.

I'll try to recreate the problem on my end in a smaller application (why do all these subtle problems come up in bigger applications
:)? )

- Mark
 
Ok, if you have any further progress, please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top