简体   繁体   English

C# InvokeMember - MissingMethodException:“未找到方法”

[英]C# InvokeMember - MissingMethodException: 'Method not found'

In a .NET Core 6 RestAPI application, I'm tracking global stock exchanges and have an instance of the below Exchange class called "ex".在 .NET Core 6 RestAPI 应用程序中,我正在跟踪全球证券交易所并有一个名为“ex”的以下交易所 class 的实例。

public class Exchange
{
    public string CountryCode { get; set; }
    public string Currency { get; set; }
    public string DataSourceCode { get; set; }
    public int DaysHoursId { get; set; }
    public string ExchangeCode { get; set; }
    public string ExchangeName { get; set; }
    public int Id { get; set; }
    public bool IsActive { get; set; }
    public DateTimeOffset LastUpdatedDate { get; set; }
    public string LastUpdatedStatus { get; set; }
    public DateTimeOffset NextUpdateDateTime { get; set; }
    public string Region { get; set; }
    public string Timezone { get; set; }
    public string Type { get; set; }
    public string Url { get; set; }
    public string Website { get; set; }
}

All fields are populated with non-null data.所有字段都填充有非空数据。 In this case, the body of my Postman call is: { "CountryCode": "USA", "Currency": "USD", "DataSourceCode": "IA2", "DaysHoursId": 2, "ExchangeCode": "AMEX", "ExchangeName": "AMEX",在这种情况下,我的 Postman 调用的主体是:{ "CountryCode": "USA", "Currency": "USD", "DataSourceCode": "IA2", "DaysHoursId": 2, "ExchangeCode": "AMEX" , "交易所名称": "AMEX",
"IsActive": true, "LastUpdatedDate": "2022-04-24T15:42:28.2533333+00:00", "LastUpdatedStatus": "OK", "NextUpdateDateTime": "2022-04-24T15:42:28.2533333+00:00", "Region": "NORTH AMERICA", "Timezone": "EST", "Type": "STOCK", "Url": "https://www.nyse.com/markets/nyse-american", "Website": "www.AMEX.com" } "IsActive": true, "LastUpdatedDate": "2022-04-24T15:42:28.2533333+00:00", "LastUpdatedStatus": "OK", "NextUpdateDateTime": "2022-04-24T15:42:28.2533333+00 :00", "Region": "NORTH AMERICA", "Timezone": "EST", "Type": "STOCK", "Url": "https://www.nyse.com/markets/nyse-american" , "网站": "www.AMEX.com" }

Using Reflection, I create a DataTable from this object with all columns except for "Id", which is an Identity primary key in the table to which data will be loaded, as follows:使用反射,我从这个 object 创建了一个数据表,其中包含除“Id”之外的所有列,它是表中将加载数据的身份主键,如下所示:

 PropertyDescriptorCollection props = TypeDescriptor.GetProperties(ex);
 DataTable dt = new DataTable();
 foreach (PropertyDescriptor p in props)
 {
      if (p.Name != "Id")  // insert into an identity column not allowed.
      { 
           dt.Columns.Add(p.Name, p.PropertyType);
      }
 }

I then attempt to create a row in the dt datatable, as follows:然后我尝试在 dt 数据表中创建一行,如下所示:

DataRow dr = dt.NewRow();
Type t = ex.GetType();
for (int i = 0; i <= dt.Columns.Count; i++)
{
    t.InvokeMember(dt.Columns[i].ColumnName, System.Reflection.BindingFlags.SetProperty, null, ex, new object[] { dt.Columns[i].ColumnName });
}

... which, when executed and traced, InvokeMember successfully iterated through the CountryCode, Currency, and DataSourceCode columns, but then - when it reaches DaysHoursId - threw a "System.MissingMethodException: 'Method MarketApi.Core.Entitites.Exchange.DaysHoursId' not found' exception. ...在执行和跟踪时,InvokeMember 成功迭代了 CountryCode、Currency 和 DataSourceCode 列,但是随后 - 当它到达 DaysHoursId - 抛出“System.MissingMethodException:'Method MarketApi.Core.Entitites.Exchange.DaysHoursId'未找到'异常。

Any insight as to what I'm doing wrong would be very much appreciated!任何关于我做错了什么的见解将不胜感激!

It throws for DaysHoursId because it is the first non-string Property of your class Exchange.它会抛出DaysHoursId ,因为它是 class Exchange 的第一个非字符串属性。 The problem is that you set the property names also as their values .问题是您将属性名称也设置为它们的

Works for CountryCode, Currency, DataSourceCode , that expect each a string value.适用于CountryCode、Currency、DataSourceCode ,它们都需要一个字符串值。 But crashes on DaysHoursId .但在DaysHoursId上崩溃。 The binder looks for a string property setter method of name/signature set_DaysHoursId(string) which actually doesn't exist.活页夹查找实际上不存在的名称/签名set_DaysHoursId(string)的字符串属性设置方法。

You rather want to assign the values in that loop:您宁愿在该循环中分配值:

DataRow dr = dt.NewRow();
Type t = ex.GetType();
//TODO fill your data in here
object [] values = new [dt.Columns.Count] { /* "de", "EUR", ... */};

for (int i = 0; i <= dt.Columns.Count; i++)
{
    t.InvokeMember(dt.Columns[i].ColumnName, System.Reflection.BindingFlags.SetProperty, null, ex, 
                  new [] { values [i] });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM