使用自定义Get方法的getter类的返回属性

时间:2019-08-27 23:22:24

标签: c# reflection

我有一个TransactionData2ClassAction子类,看起来像这样:

  public class TransactionData2ClassAction : Entity
  {
    public int       TransactionId        => Get<int>();
    public int       FundId               => Get<int>();
    public string    PortfolioCode        => Get<string>();
    public string    SecuritySymbol       => Get<string>();
    public string    SecurityTypeCode     => Get<string>();
    public DateTime  TradeDate            => Get<DateTime>();
    public DateTime? SettleDate           => Get<DateTime?>();
    public decimal   Quantity             => Get<decimal>();
    public decimal   Amount               => Get<decimal>();
    public decimal   SecFee               => Get<decimal>();
    public string    BrokerCode           => Get<string>();
    public short     TaxLotNumber         => Get<short>();
    public string    TransactionCode      => Get<string>();
    public decimal   CommissionAmount     => Get<decimal>();

    public int TestProperty { get; }
  }

Entity基类如下所示:

public class Entity : INotifyPropertyChanged, IDataErrorInfo
  {
    protected T Get<T>([CallerMemberName] string aColumnName = null, DataRowVersion? aDataRowVersion = null)
    {
      // Details.
    }

    protected string Get(CompressionEncryptionOptions aOption, [CallerMemberName] string aColumnName = null)
    {        
      // Details.
    }        
  }

我的问题是我该如何返回使用Entity基类Get方法之一的吸气剂的属性?

例如,结果将返回除TestProperty之外的所有属性。

到目前为止,我已经拥有了:

public static PropertyInfo[] GetTransactionData2FieldsFromEntityUsingProperties(Type aEntityType)
{
  if (!aEntityType.IsSubclassOf(typeof(Entity)))
    throw new InvalidOperationException("Wrong type.");

  return aEntityType.GetProperties().Where(aProperty => aProperty.CanRead && aProperty.GetAccessors().Any(aMethodInfo => aMethodInfo.IsAssembly || aMethodInfo.IsPublic)).ToArray();
}

0 个答案:

没有答案