本文介绍了如何在下拉列表中显示WCF中可用的所有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在下拉列表中显示WCF中可用的所有方法.我只需要显示那些暴露给客户端的方法.我有下面的代码工作,但它显示了比预期更多的方法.似乎显示了所有内容.

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

推荐答案

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

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);
}

这篇关于如何在下拉列表中显示WCF中可用的所有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 09:57