为了账号安全,请及时绑定邮箱和手机立即绑定

通过属性名称获取通用代码中的value / obj ref

通过属性名称获取通用代码中的value / obj ref

C#
慕妹3146593 2021-05-02 20:16:17
我最近创建了通用sql命令生成代码。一切正常,但我目前正在努力解决的一件事。如何通过给定的属性名称获取实例/ Answer属性的值?是)我有的:问题实例作为对象(例如myObject)问题的属性+内部道具名称(字符串)+内部类类型[Table("Question")]public class Question{    [Column("QuestionID", true)]    public int QuestionId { get; set; }    [Column("Number")]    public string Nr { get; set; }    [Column("CorrectAnswer", typeof(Answer), nameof(Answer.Text))]    public Answer CorrectAnswer { get; set; }}[AttributeUsage(AttributeTargets.Property, Inherited = false)]public class ColumnAttribute : DatabaseAttribute{    public bool IsTechnicalId { get; private set; } = false;    public string DeepPropName { get; private set; } = null;    public Type DeepClass { get; private set; } = null;    public bool HasDeepProp { get; private set; } = false;    public ColumnAttribute(string name) : base(name)    {    }    /// <summary>    /// Creates a deep object property column definition    /// </summary>    /// <param name="name"></param>    /// <param name="deepProperty"></param>    public ColumnAttribute(string name, Type deepClass, string deepPropertyName) : base(name)    {        DeepPropName = deepPropertyName;        DeepClass = deepClass;        HasDeepProp = true;    }    public ColumnAttribute(string name, bool isTechnicalId) : this(name)    {        IsTechnicalId = isTechnicalId;    }}我当前用于获取道具和值的代码如下所示。   一切都可以正常运行,唯一的例外是:如何获取要搜索的对象内另一个对象的属性值?我可以获取实例对象属性的任何实例吗?Edit1:我可以通过if(pInfo.Name == "CorrectAnswer")    answerRef = pInfo.GetValue(obj, null);然后从answerRef角色获取道具,以获取给定propName的值
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 138 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信