Pergunta

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?

Foi útil?

Solução

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();

Outras dicas

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 :)

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 ...
    }
}

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; }   
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top