Extension method and Explicit casting

丶灬走出姿态 提交于 2020-01-02 02:04:58

问题


I'm using class from some assembly(source code is not available), so it is not possible to change their's code I need to add extension method for explicit cast operator, is there any way to achieve that? (I have tried to add as regular extension method, but without success)

 public static explicit operator MembershipUser(this MembershipUser membership, User user)
    {
        return new MembershipUser("SimplyMembershipProvider", user.UserName, user.UserId, user.Email, null, null, user.IsApproved, user.IsLocked,
            user.CreateDate, user.LastLoginDate, user.LastActivityDate, user.CreateDate, DateTime.MinValue);
    }

how can i solve this?


回答1:


You cannot overload operators via Extension methods.

Best you can do with an extension method:

public static MembershipUser ConvertToMembershipUser(this User user)
{
    return new MembershipUser("SimplyMembershipProvider", 
                              user.UserName, 
                              user.UserId, 
                              user.Email, 
                              null, 
                              null, 
                              user.IsApproved, 
                              user.IsLocked,
                              user.CreateDate, 
                              user.LastLoginDate, 
                              user.LastActivityDate,
                              user.CreateDate, 
                              DateTime.MinValue);
}

MembershipUser membershipUser = aUser.ConvertToMembershipUser();



回答2:


As others have said, you can't overload operators using extension methods - but a simple extension method would still be useful:

public static MembershipUser ToMembershipUser(this User user)
{
    return new MembershipUser("SimplyMembershipProvider",
        user.UserName, user.UserId, user.Email, null, null, 
        user.IsApproved, user.IsLocked, user.CreateDate, 
        user.LastLoginDate, user.LastActivityDate, user.CreateDate,
        DateTime.MinValue);
}

Then just use it as:

User user = ...;
MembershipUser membershipUser = user.ToMembershipUser();

Personally I find this clearer than using an explicit conversion anyway - I very rarely go in for operator overloading. In particular, given that the classes certainly sound related, a naive user might expect this code:

User user = ...;
MembershipUser membershipUser = (MembershipUser) user;

... to be a normal reference conversion, expecting MembershipUser to be a class derived from User. This is important in terms of object identity - if that were the case, it wouldn't be changing the object involved (and changes to the object referred to by user would still be visible via membershipUser).

Creating a ToMembershipUser method makes it clearer that you're converting from one form to a new object of the other type. All IMO, of course :)




回答3:


No, you cannot add an extension conversion.

Explicit or implicit conversions may only be defined if you control the source code for one of the types involved. In your case, you can control either MembershipUser or User, you do not need to control both. But you must control one.

If you control neither, you'll simply need to define the conversion in a method of some other class whose job it is to control such a conversion.

class UserConverter 
{
    public MembershipUser ConvertFrom(User user) 
    {
        return ...
    }
}



回答4:


Not saying this is a good practice (read other answers for why you should not want this, but if the classes are not sealed, this is possible, just not using extension methods).

Basically, you can create a surrogate class inheriting User who defines the casting operator, something like:

    class UserSurrogate : User
    {
        public static explicit operator UserSurrogate(MemberShipUser other)
        {
            return  new UserSurrogate() { Name = other.Name };
        }
    }

    class User
    {
        public string Name { get; set; }
    }

    class MemberShipUser
    {
        public string Name { get; set; }   
    }


来源:https://stackoverflow.com/questions/8306614/extension-method-and-explicit-casting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!