I am trying to cast the type skill to type SkillViewModel.
This is where I try the cast:
SkillViewModel skillViewModel = (SkillViewModel)skill;
This is my skill model that I am trying to convert:
public class Skill
{
public int SkillId { get; set; }
public string SkillName { get; set; }
public string SkillDescription { get; set; }
public int SkillLevelId { get; set; }
public string SkillLevelDescription { get; set; }
public int LevelId { get; set; }
public int LevelNumber { get; set; }
}
This is the SkillViewModel that I am trying to convert to. It inherits skill and has one added property RoleId:
public class SkillViewModel : Skill
{
public int RoleId { get; set; }
}
===========================================================================
Rather than casting the skill object to type SkillViewModel, I just created a new instance and explicitly copied each in the skill to the SkillViewModel added the roleId at the end.
Like this:
private SkillViewModel[] GetRoleSpecificSkills(Skill[] skills, int roleId)
{
var results = new List<SkillViewModel>();
foreach (var skill in skills)
{
SkillViewModel skillViewModel = new SkillViewModel();
skillViewModel.SkillId = skill.SkillId;
skillViewModel.SkillName = skill.SkillName;
skillViewModel.SkillDescription = skill.SkillDescription;
skillViewModel.SkillLevelId = skill.SkillLevelId;
skillViewModel.SkillLevelDescription = skill.SkillLevelDescription;
skillViewModel.LevelId = skill.LevelId;
skillViewModel.LevelNumber = skill.LevelNumber;
skillViewModel.RoleId = roleId;
results.Add(skillViewModel);
}
return results.ToArray();
}
You can't cast parent to child this way. And this is very bad practice.
If you really need to downcast, try this:
var jsonParent = JsonConvert.SerializeObject(parent);
Child c = JsonConvert.DeserializeObject<Child>(jsonParent);
Generally, you cannot cast from an incompatible type (such as a parent class) in C# nor any other statically typed programming language to that extent: a Skill
object has a different representation in memory than a SkillViewModel
object: Skill
's memory region is smaller by the size of the RoleId
property, so if such casting is allowed by the compiler of a given language at all, then later if you try to access RoleId
, it will result in a memory violation, because the offset of RoleId
is beyond the boundary of the memory region allocated for the Skill
object.
In case of converting from a parent class to a subclass, the best way is to create a constructor in SkillViewModel
that takes a Skill
object as a param, copies all its properties to its own instance variables and assigns some sane default value to the RoleId
.
Note however that all these properties will now be referenced from 2 objects, so if any of them is mutable, then changing it in 1 object will also change it the second: this may have unexpected results sometimes, hence immutable objects are generally preferred whenever possible. Whenever you are making a new reference to a mutable object, you should consider making a deep clone of it instead: take into consideration performance and memory usage penalty on one hand and likeliness of someone making a bug on the other.
当您像这样投射时,您应该能够访问 RoleID 属性:
SkillViewModel skillViewModel = skill as SkillViewModel;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.