I am trying to learn about the Orchard MVC application. I see the following code but cannot understand what it is doing. Can someone explain what this:
User.As<UserPart>().Record.UserName = value;
means?
public class UserEditViewModel {
[Required]
public string UserName {
get { return User.As<UserPart>().Record.UserName; }
set { User.As<UserPart>().Record.UserName = value; }
}
[Required]
public string Email {
get { return User.As<UserPart>().Record.Email; }
set { User.As<UserPart>().Record.Email = value; }
}
public IContent User { get; set; }
}
Not knowing the source, it looks like the call to
As<type>()is equivalent toobj as typei.e. the object is being cast as a type
type—UserPartin this case. SoUseris defined asIContentbut in this case it is assumed that the class implementing that interface is of typeUserPartand can be safely converted to that type.So
User.As<UserPart>()returns an object of typeUserPartwhich presumably has a propertyRecordon it.It seems odd to me, because why abstract the
IContentinterface if you are tying the implementation to the typeUserPart, but again, I don’t know the code and it could well make sense in the greater context.edit: I say tying the implementation, because presumably
User.As<UserPart>()could return null if the underlying class is not of typeUserPart, and there’s no check for that.