Reputation: 312
I have two classes, one called MemberModel and one called CustomPrincipal. The CustomPrincipal class inheritances the MemberModel class.
The MemberModel class looks like this:
namespace example.Models
{
public class MemberModel
{
[Required]
public string Username { get; set; }
[Required]
public string Password { get; set; }
public bool Remember { get; set; }
}
}
The CustomPrincipal class looks like this:
namespace example.Examples
{
public class CustomPrincipal : MemberModel, IPrincipal
{
public CustomPrincipal(IIdentity identity)
{
this.Identity = identity;
}
public IIdentity Identity { get; private set; }
public bool IsInRole(string role) { return false; }
}
}
In the example below you see two classes MemberModel what is filled with the username, password and remember and the CustomPrincipal class what is filled with the IIDentity information BUT not the username, password and remember.
JavaScriptSerializer serializer = new JavaScriptSerializer();
MemberModel memberModel = serializer.Deserialize<MemberModel>(authTicket.UserData);
IIdentity user = HttpContext.Current.User.Identity;
CustomPrincipal customPrincipal = new CustomPrincipal(user);
Now I want the attributes from the MemberModel to be merged with the attributes of the CustomPrincipal.
I have tryed multiple things but none of them are working. I tryed to cast the the MemberModel to the CustomPrincipal, but unfortunately this did not work (see below).
customPrincipal = (CustomPrincipal) memberModel;
Unable to cast object of type 'example.Examples.Models.MemberModel' to type 'example.Examples.CustomPrincipal'.
I have also tryed the example of Rob Harley, that uses reflection to merge the two object, but this also did not work.
Upvotes: 2
Views: 1700
Reputation: 312
Finally I have fixed it using reflection on the right way.
PropertyInfo[] props = typeof(MemberModel).GetProperties();
foreach (PropertyInfo prop in props)
{
if (prop.Name != "Password")
customPrincipal.GetType().GetProperty(prop.Name).SetValue(customPrincipal, serializeModel.GetType().GetProperty(prop.Name).GetValue(memberModel, null) as string);
}
First we get all the propeties from the MemberModel, then we loop through all these properties and set the value of the customPrincipal by the value of the MemberModel.
Upvotes: 0
Reputation: 5141
The rules of inheritance won't allow you cast a base class (MemberModel
) into a child class (CustomPrincipal
). You're only allowed to cast a child class back into a base..
In example,
(DOG, CAT) => ANIMAL
I have a dog and a cat and I know that both of them are animals. Therefore, I can treat the dog and the cat as an animal.
CAT => ANIMAL => DOG
Let's say the cat is an animal but you want to turn that animal into a dog, you cannot! Why? because you don't really know if it is a dog or not.
Best you can do is add a constructor overload or a method that returns a CustomPrincipal
from a MemberModel
.
public CustomPrincipal(MemberModel model)
{
this.Username = model.Username;
...
}
or
public static CustomPrincipal FromMember(MemberModel model)
{
return new CustomPrincipal()
{
Username = model.Username,
...
}
}
Upvotes: 5