问题
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
回答1:
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);
}
回答2:
foreach (var method in methods)
{
// Add the line below
if (method.GetCustomAttributes(typeof(OperationContractAttribute)).Length > 0)
{
string methodName = method.Name;
ddlMethods.Items.Add(methodName);
}
}
来源:https://stackoverflow.com/questions/4169096/how-can-i-show-all-the-methods-that-are-available-in-my-wcf-in-a-dropdown