user1181942
user1181942

Reputation: 1607

Inherit Properties of Entity class into another

I have Employee Entity as

public class Employee
{
    public Employee();

    public int BossId { get; }
    public int BossUserId { get; }
    public string CostCentre { get; }
    public int CostCentreId { get; }
    public string Department { get; }
    public int DepartmentId { get; }
    public string Designation { get; }
    public int DesignationId { get; }
    public string EmailAddress { get; }
    public int EmployeeId { get; }
    public string FirstName { get; }
    public string FullName { get; }
    public string LastName { get; }
    public string LoginId { get; }
    public int UserId { get; }

    public override string ToString();
}

I want to use all these properties in another entity and assign values to them.

My another entity is as

public class UserRoles
{
    public UserRoles()
    {
    }

    public int UserRoleId { get; set; }
    public long EmpUserId { get; set; }
    public int RoleId { get; set; }
    public DateTime AddedOn { get; set; }
    public long AddedBy { get; set; }
    public long ModifiedBy { get; set; }
    public DateTime ModifiedOnd { get; set; }
  }

If i create property of Employee class, I am not able to assign values to it. And i can not change Employee class as it is in some dll.

Is there any way i can override it or inherit and assign values to these properties?

Upvotes: 0

Views: 125

Answers (1)

L-Four
L-Four

Reputation: 13551

You could encapsulate the Employee class with your own class EmployeeWrapper, and in EmployeeWrapper put logic that uses reflection to set properties of the Employee class as described in Reflection without a Getter/Setter?.

Then you use your EmployeeWrapper and pass parameters through its constructor for example, or just setting its properties, and in the back it uses reflection to set the properties of the actual Employee instance.

Upvotes: 1

Related Questions