0

在此 C# 代码中,我希望输出结果与此相同:

AccountantCopy 
Address 
AddressBlock 
CompanyAddressBlockForCustomer 
CompanyAddressForCustomer 
...
TaxForm
Type

因此,如果我在示例中使用 typeof() :

 Type MyType = typeof(ICompanyRet);                          
 MemberInfo[] Mymemberinfoarray = MyType.GetInterfaces();                
 Console.Write("\nThere are {0} members in ",Mymemberinfoarray.GetLength(0));                
 Console.Write("{0}.", MyType.Name);

 foreach (MemberInfo Mymemberinfo in Mymemberinfoarray)                
 {
      if (Mymemberinfo.Name != null)
          Console.Write("\n" + Mymemberinfo.Name);
 }

结果:

 There are 1 members in ICompanyRet.

 IQBBase

ICompanyRet 中的此代码:

using System.Runtime.InteropServices;
namespace QBFC10Lib
{
[Guid("089A0574-58A7-48AE-B17E-297095B9E311")]
[TypeLibType(4160)]
public interface ICompanyRet : IQBBase
{
    [DispId(24)]
    IAccountantCopy AccountantCopy { get; }
    [DispId(7)]
    IAddress Address { get; }
    [DispId(8)]
    IAddressBlock AddressBlock { get; }
    [DispId(11)]
    IAddressBlock CompanyAddressBlockForCustomer { get; }
    [DispId(10)]
    IAddress CompanyAddressForCustomer { get; }
    [DispId(15)]
    IQBStringType CompanyEmailForCustomer { get; }
    [DispId(5)]
    IQBStringType CompanyName { get; }
    [DispId(19)]
    IQBStringType CompanyType { get; }
    [DispId(16)]
    IQBStringType CompanyWebSite { get; }
    [DispId(25)]
    IDataExtRetList DataExtRetList { get; }
    [DispId(20)]
    IQBStringType EIN { get; }
    [DispId(14)]
    IQBStringType Email { get; }
    [DispId(13)]
    IQBStringType Fax { get; }
    [DispId(17)]
    IQBENFirstMonthFiscalYearType FirstMonthFiscalYear { get; }
    [DispId(18)]
    IQBENFirstMonthIncomeTaxYearType FirstMonthIncomeTaxYear { get; }
    [DispId(4)]
    IQBBoolType IsSampleCompany { get; }
    [DispId(9)]
    IAddress LegalAddress { get; }
    [DispId(6)]
    IQBStringType LegalCompanyName { get; }
    [DispId(12)]
    IQBStringType Phone { get; }
    [DispId(21)]
    IQBStringType SSN { get; }
    [DispId(23)]
    ISubscribedServices SubscribedServices { get; }
    [DispId(22)]
    IQBENTaxFormType TaxForm { get; }
    [DispId(1)]
    IObjectType Type { get; }
}
}

对不起,这是我的第一个问题。谢谢你。

4

1 回答 1

1

您实际上想要检索接口的属性,而不是它实现的接口。

MyType.GetProperties()

应该返回您正在寻找的属性。

Type MyType = typeof(ICompanyRet);                          
PropertyInfo[] properties = MyType.GetProperties();                
Console.Write("\nThere are {0} properties in {1}", properties.Length, MyType.Name);

foreach (PropertyInfo property in properties)                
{
    Console.Write("\n" + property.Name);
}
于 2013-07-05T09:39:03.057 回答