简体   繁体   中英

Make GetType match typeof base class

I'm writing a role provider to supply user role information to an ASP.NET MVC 4 framework I'm using a library called JDash. The JDash documentation calls for me to write a provider implementing System.Web.Security.RoleProvider , then set its type name in a JDash .config file. So I wrote a stub of my role provider and gave JDash its type name, and JDash found the setting properly and tried to instantiate my RoleProvider . However, it kept failing with:

"Custom role provider must implement System.Web.Security.RoleProvider abstract class"

Even though I was absolutely sure that my class was implementing RoleProvider properly.

So I took a look at the JDash source code and found how it's checking that the set role provider implements RoleProvider :

Type type = Type.GetType(config["roleProvider"]);
if (!(type == typeof (RoleProvider)))
      throw new ArgumentException("Custom role provider must implement System.Web.Security.RoleProvider abstract class");

The problem is, GetType() returns the actual runtime type of my role provider...which is not System.Web.Security.RoleProvider , it's a class derived from System.Web.Security.RoleProvider , so of course it doesn't match typeof(RoleProvider) . My class and RoleProvider are of different types; one just happens to be derived from the other, a fact that typeof happily ignores. So if I'm looking at this right, JDash has a bug that makes it impossible to give it a role provider that returns its runtime type as something other than RoleProvider.

I'll be reporting this bug and hoping for a fix, but for the time being, is there some way I can work around it? Could I somehow make GetType("MyRoleProviderDerivedClass") return the type of RoleProvider ? Or can I trick typeof somehow, so that it thinks MyRoleProviderDerivedClass is the same as the RoleProvider base class?

There is no way around this because you can't hook into the comparison or into typeof . This is a simple reference comparison.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM