mardi 29 novembre 2016

user control reflection type is different than class type, causing type equality failure

I have a collection of user controls that I want to get out of a screen and do something with. What I've found is that using the 'is' keyword works, but doing a type comparison fails. E.g.

myUserControl is MyUserControl  
true  

myUserControl.GetType() == typeof(MyUserControl)  
false  

When I inspected the user control using reflection, I found that the user control from the screen has a full name in the format of

Project.folder_folder_control_ascx  

whereas the class of the user control is

Project.folder.folder.control  

But none of the .NET controls work this way. Their GetType().FullName is the regular class namespace. To get the dot format, you have to go to the base type:

myUserControl.GetType().BaseType == typeof(MyControl)  
true

I was able to get the controls that I needed by using 'is' for each type I wanted, rather than putting all of the types in a list. Is there a resolution for this situation, since you can't do

controls.Where(control => validTypes.Any(vt => control is vt))  

I'm guessing the reasoning has to do with UserControls being partial classes and whatnot, but I'm not sure what's happening or why or if there's a way to get GetType() == typeof(t) to pass.





Aucun commentaire:

Enregistrer un commentaire