如何在下拉列表中显示 WCF 中可用的所有方法

发布于 2024-10-02 08:22:35 字数 491 浏览 1 评论 0原文

如何在下拉列表中显示 WCF 中可用的所有方法。我只需要显示那些向客户端公开的方法。我可以使用以下代码,但它显示的方法比预期多得多。好像显示了全部。

MethodInfo[] methods = typeof(IntlService.ClientDataServiceClient).GetMethods();

//// sort methods by name
Array.Sort(methods,
        delegate(MethodInfo methods1, MethodInfo methods2)
        { return methods1.Name.CompareTo(methods2.Name); });

foreach (var method in methods)
{
    string methodName = method.Name;
    ddlMethods.Items.Add(methodName);
}

如何限制显示仅显示我定义的内容

How can I show all the methods that are available in my WCF in a dropdown. I need to show only those methods that are exposed to the client. I have the following code working, but it displays much more methods than expected. Seems it displays all.

MethodInfo[] methods = typeof(IntlService.ClientDataServiceClient).GetMethods();

//// sort methods by name
Array.Sort(methods,
        delegate(MethodInfo methods1, MethodInfo methods2)
        { return methods1.Name.CompareTo(methods2.Name); });

foreach (var method in methods)
{
    string methodName = method.Name;
    ddlMethods.Items.Add(methodName);
}

How can I restrict the display to show only the ones that I defined

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命硬 2024-10-09 08:22:35

如果您只想获取类定义的方法(在本例中为 IntlService.ClientDataServiceClient),则可以像这样更改对 GetMethods() 的调用:

MethodInfo[] methods = typeof(IntlService.ClientDataServiceClient).GetMethods(BindingFlags.DeclaredOnly);

如果您'如果您希望仅获取声明为服务方法的方法,那么您需要检查这些方法的属性:

MethodInfo[] methods = typeof(IntlService.ClientDataServiceClient).GetMethods(BindingFlags.DeclaredOnly);

// sort here...

foreach( var method in methods )
{
    if( method.GetCustomAttributes(typeof(System.ServiceModel.OperationContractAttribute), true).Length == 0 )
        continue;

    string methodName = method.Name;
    ddlMethods.Items.Add(methodName);
}

If you're looking to get only methods defined by your class, in this case, IntlService.ClientDataServiceClient, then alter your call to GetMethods() like this:

MethodInfo[] methods = typeof(IntlService.ClientDataServiceClient).GetMethods(BindingFlags.DeclaredOnly);

If you're looking to get only methods that are declared as service methods, then you'll need to examine the attributes on the methods:

MethodInfo[] methods = typeof(IntlService.ClientDataServiceClient).GetMethods(BindingFlags.DeclaredOnly);

// sort here...

foreach( var method in methods )
{
    if( method.GetCustomAttributes(typeof(System.ServiceModel.OperationContractAttribute), true).Length == 0 )
        continue;

    string methodName = method.Name;
    ddlMethods.Items.Add(methodName);
}
林空鹿饮溪 2024-10-09 08:22:35
foreach (var method in methods)
{
   // Add the line below
   if (method.GetCustomAttributes(typeof(OperationContractAttribute)).Length > 0)
   {
      string methodName = method.Name;
      ddlMethods.Items.Add(methodName);
   }
}
foreach (var method in methods)
{
   // Add the line below
   if (method.GetCustomAttributes(typeof(OperationContractAttribute)).Length > 0)
   {
      string methodName = method.Name;
      ddlMethods.Items.Add(methodName);
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文