下面是我的代码。我想获取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的值,请帮助
答案 0 :(得分:0)
这似乎是最简单的答案:
public interface IValidationEntity
{
void Run(CtxTest context, IValidationEntity entity);
string Details { get; }
}
这将需要显式实现,以允许您的类具有不同的实现名称(即string EventDetails
),同时仍然符合IValidationEntity
接口。