Implicit/Explicit conversion with respect to the “as” keyword

前端 未结 4 1814
梦谈多话
梦谈多话 2020-12-18 09:36

I\'m trying to do some unit testing on a project that unfortunately has high level of unit interdependence. Currently, a lot of our classes look to a custom UserId

4条回答
  •  青春惊慌失措
    2020-12-18 10:26

    So, why don't you use an explicit cast?

    // will throw if cast fails
    internal static UserIdentity GetCurrentIdentity()
    {
        UserIdentity currentIdentity = (UserIdentity) ApplicationContext.User.Identity ;
        return currentIdentity;
    }
    

    This ought to trigger your explicit operator. You can test with is first to make it safer.

提交回复
热议问题