猿问

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

我最近创建了通用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的值


慕妹3146593
浏览 139回答 1
1回答
随时随地看视频慕课网APP
我要回答