Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extension method and Explicit casting

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?

like image 979
Artur Keyan Avatar asked Nov 29 '11 06:11

Artur Keyan


People also ask

What is the meaning of extension method?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

What is an advantage of using extension methods?

The main advantage of the extension method is to add new methods in the existing class without using inheritance. You can add new methods in the existing class without modifying the source code of the existing class. It can also work with sealed class.

What is extension method in MVC?

What is extension method? Extension methods in C# are methods applied to some existing class and they look like regular instance methods. This way we can "extend" existing classes we cannot change. Perhaps the best example of extension methods are HtmlHelper extensions used in ASP.NET MVC.

What is extension method in LINQ?

LinqExtensionMethod.zip. Linq provides standard query operators like filtering, sorting, grouping, aggregation, and concatenations, and it has many operators to achive many types of functionalities, which are called extension methods, in LINQ.


1 Answers

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();
like image 158
Christopher Currens Avatar answered Oct 11 '22 22:10

Christopher Currens