开发者

C# cast type help

开发者 https://www.devze.com 2023-03-20 15:47 出处:网络
I wrote a test method on my inherited Membership Provider object a开发者_开发问答nd want to call that method by actually casting up the membership provider to my object. Membership is not of type Memb

I wrote a test method on my inherited Membership Provider object a开发者_开发问答nd want to call that method by actually casting up the membership provider to my object. Membership is not of type MembershipProvider. It is the default Membership object.

 ((MyMembershipProvider)Membership).Test();

Thanks in advance for the help.


Sure it is possible. But if you are implementing custom membership provider, then you forgot to take property Provider from your Membership object. Like this:

((MyMembershipProvider)Membership.Provider).Test();


Try this:

((Membership)MyMembershipProvider).Test();

I'm guessing here, but I think your MemberShip is the class type and the MyMembershipProvider is a reference to a class (an object). You've just got your cast operands backwards.

In otherwords, you may be trying to do something like this:

int myVal = 1;
double mySecondVal = (myVal)double; // fail

double mySecondVal = (double)myVal; // success


It is possible, if the Membership object is a MyMembershipProvider type, and it has the .Test() method. You can check this like that:

if (Membership is MyMembershipProvider)
    (Membership as MyMembershipProvider).Test();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号