如何使用C#获取基类型类值

问题描述 投票:0回答:1

以下是我的代码。我想获得Test.Details和Test.Events的值

public partial class Test : BaseTypes.ValidationEntityBase
{
    public bool Active { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Test()
    {
        Events = new HashSet<Event>();
    }

    [Key]
    [StringLength(20)]
    public string ID { get; set; }

    [Required()]
    [StringLength(50)]
    public string Details { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    [XLTargetName(null)]
    public virtual ICollection<Event> Events { get; set; }

    public override void AddToModel(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Test>().HasMany(e => e.Events).WithRequired(e => e.Test).WillCascadeOnDelete(false);
    }
}

public partial class Events : BaseTypes.ValidationEntityBase
{
    public Events()
    {
        Active = true; //DEFAULT TO ACTIVE
    }

    [Key()]
    public string EventID { get; set; }

    [Required()]
    [StringLength(20)]
    public string ID { get; set; }

    [Required()]
    [StringLength(50)]
    public string EventDetails { get; set; }

    [XLTargetName(null)]
    [JsonIgnore]
    [ScriptIgnore]
    public virtual Test Test { get; set; }

    public override void AddToModel(System.Data.Entity.DbModelBuilder modelBuilder)
    {
    }
}

public abstract class ValidationEntityBase : IValidationEntity
{

    public ValidationEntityBase()
    {
        Valid = true;
    }

    public virtual void Run(CtxTest context, IValidationEntity entity)
    {
    }
}

public interface IValidationEntity
{
    void Run(CtxTest context, IValidationEntity entity);
}

这是我的业务对象

public void RunRules(string typeCode)
{
    var inputRules = Rule.FindRules(this.GetContext(), typeCode);
    foreach (var rule in inputRules)
    {
        rule.Run<Test>(this.GetContext(), this.Test, "Sample", typeCode);
    }
}

我的规则类:

public void Run<T>(CtxTest context, T entity, string sample, string typeCode) where T : class, IValidationEntity
{
    var validation = this.GetExecutionType();
    var execution = (IValidationEntity)Activator.CreateInstance(validation, sample);
    execution.Run(context, entity);
}

每当运行规则然后它将来到下面的类,我得到所有的基类型(测试类)值

public class Person : ValidationEntityBase
{
    public Person(string msgTypeCode)
    {
        MESSAGE_TYPECODE = msgTypeCode;
    }

    public override void Run(Context.CtxTest context, IValidationEntity entity)
    {
    }
}

如何在run方法中从IValidationEntity实体打印Test.Details&Test.Events的值,请帮忙

c# entity-framework
1个回答
0
投票

这似乎是最简单的答案:

public interface IValidationEntity
{
    void Run(CtxTest context, IValidationEntity entity);
    string Details { get; }
}   

它需要明确实现,以允许您的类具有不同的实现名称(即string EventDetails),同时仍然符合IValidationEntity接口。

© www.soinside.com 2019 - 2024. All rights reserved.