public static void Main() { Type myType = typeof(MyService); MethodInfo myMethodInfo = myType.GetMethod("Add"); // Create a synchronous 'LogicalMethodInfo' instance. LogicalMethodInfo myLogicalMethodInfo = (LogicalMethodInfo.Create(new MethodInfo[] { myMethodInfo }, LogicalMethodTypes.Sync))[0]; // Display the method for which the attributes are being displayed. Console.WriteLine("\nDisplaying the attributes for the method : {0}\n", myLogicalMethodInfo.MethodInfo); // Displaying a custom attribute of type 'MyAttribute' Console.WriteLine("\nDisplaying attribute of type 'MyAttribute'\n"); object attribute = myLogicalMethodInfo.GetCustomAttribute(typeof(MyAttribute)); Console.WriteLine(((MyAttribute)attribute).Name); // Display all custom attribute of type 'MyAttribute'. Console.WriteLine("\nDisplaying all attributes of type 'MyAttribute'\n"); object[] attributes = myLogicalMethodInfo.GetCustomAttributes(typeof(MyAttribute)); for (int i = 0; i < attributes.Length; i++) { Console.WriteLine(((MyAttribute)attributes[i]).Name); } // Display all return attributes of type 'MyAttribute'. Console.WriteLine("\nDisplaying all return attributes of type 'MyAttribute'\n"); ICustomAttributeProvider myCustomAttributeProvider = myLogicalMethodInfo.ReturnTypeCustomAttributeProvider; if (myCustomAttributeProvider.IsDefined(typeof(MyAttribute), true)) { attributes = myCustomAttributeProvider.GetCustomAttributes(true); for (int i = 0; i < attributes.Length; i++) { if (attributes[i].GetType().Equals(typeof(MyAttribute))) { Console.WriteLine(((MyAttribute)attributes[i]).Name); } } } // Display all the custom attributes of type 'MyAttribute'. Console.WriteLine("\nDisplaying all attributes of type 'MyAttribute'\n"); myCustomAttributeProvider = myLogicalMethodInfo.CustomAttributeProvider; if (myCustomAttributeProvider.IsDefined(typeof(MyAttribute), true)) { attributes = myCustomAttributeProvider.GetCustomAttributes(true); for (int i = 0; i < attributes.Length; i++) { if (attributes[i].GetType().Equals(typeof(MyAttribute))) { Console.WriteLine(((MyAttribute)attributes[i]).Name); } } } }
public void BeginEndMethodInfo() { LogicalMethodInfo [] ll = LogicalMethodInfo.Create( new MethodInfo [] { typeof(FooService).GetMethod("BeginEcho"), typeof(FooService).GetMethod("EndEcho") }); Assert.AreEqual(1, ll.Length, "#1"); LogicalMethodInfo l = ll [0]; Assert.IsNull(l.MethodInfo, "#2"); Assert.IsNotNull(l.BeginMethodInfo, "#3"); Assert.IsNotNull(l.EndMethodInfo, "#4"); }
internal static LogicalMethodInfo[] GetMethods(Type type) { ArrayList list = new ArrayList(); MethodInfo[] methods = type.GetMethods(); for (int i = 0; i < methods.Length; i++) { if (methods[i].GetCustomAttributes(typeof(WebMethodAttribute), false).Length > 0) { list.Add(methods[i]); } } return(LogicalMethodInfo.Create((MethodInfo[])list.ToArray(typeof(MethodInfo)))); }
public static void Main() { Type myType = typeof(MyService); MethodInfo myMethodInfo = myType.GetMethod("MyMethod"); LogicalMethodInfo myLogicalMethodInfo = (LogicalMethodInfo.Create(new MethodInfo[] { myMethodInfo }))[0]; Console.WriteLine("\nPrinting parameters for the method : {0}", myLogicalMethodInfo.Name); Console.WriteLine("\nThe parameters of the method {0} are :\n", myLogicalMethodInfo.Name); ParameterInfo[] myParameters = myLogicalMethodInfo.Parameters; for (int i = 0; i < myParameters.Length; i++) { Console.WriteLine("\t" + myParameters[i].Name + " : " + myParameters[i].ParameterType); } Console.WriteLine("\nThe in parameters of the method {0} are :\n", myLogicalMethodInfo.Name); myParameters = myLogicalMethodInfo.InParameters; for (int i = 0; i < myParameters.Length; i++) { Console.WriteLine("\t" + myParameters[i].Name + " : " + myParameters[i].ParameterType); } Console.WriteLine("\nThe out parameters of the method {0} are :\n", myLogicalMethodInfo.Name); myParameters = myLogicalMethodInfo.OutParameters; for (int i = 0; i < myParameters.Length; i++) { Console.WriteLine("\t" + myParameters[i].Name + " : " + myParameters[i].ParameterType); } if (myLogicalMethodInfo.IsVoid) { Console.WriteLine("\nThe return type is void"); } else { Console.WriteLine("\nThe return type is {0}", myLogicalMethodInfo.ReturnType); } }
public static void Main() { Type myType = typeof(MyService); MethodInfo myBeginMethod = myType.GetMethod("BeginAdd"); MethodInfo myEndMethod = myType.GetMethod("EndAdd"); LogicalMethodInfo myLogicalMethodInfo = (LogicalMethodInfo.Create(new MethodInfo[] { myBeginMethod, myEndMethod }, LogicalMethodTypes.Async))[0]; Console.WriteLine("\nThe asynchronous callback parameter of method {0} is :\n", myLogicalMethodInfo.Name); Console.WriteLine("\t" + myLogicalMethodInfo.AsyncCallbackParameter.Name + " : " + myLogicalMethodInfo.AsyncCallbackParameter.ParameterType); Console.WriteLine("\nThe asynchronous state parameter of method {0} is :\n", myLogicalMethodInfo.Name); Console.WriteLine("\t" + myLogicalMethodInfo.AsyncStateParameter.Name + " : " + myLogicalMethodInfo.AsyncStateParameter.ParameterType); Console.WriteLine("\nThe asynchronous result parameter of method {0} is :\n", myLogicalMethodInfo.Name); Console.WriteLine("\t" + myLogicalMethodInfo.AsyncResultParameter.Name + " : " + myLogicalMethodInfo.AsyncResultParameter.ParameterType); Console.WriteLine("\nThe begin method of the asynchronous method {0} is :\n", myLogicalMethodInfo.Name); Console.WriteLine("\t" + myLogicalMethodInfo.BeginMethodInfo); Console.WriteLine("\nThe end method of the asynchronous method {0} is :\n", myLogicalMethodInfo.Name); Console.WriteLine("\t" + myLogicalMethodInfo.EndMethodInfo); if (myLogicalMethodInfo.IsAsync) { Console.WriteLine("\n{0} is asynchronous", myLogicalMethodInfo.Name); } else { Console.WriteLine("\n{0} is synchronous", myLogicalMethodInfo.Name); } }
// <Snippet1> // <Snippet2> public static void Main() { // Get the type information. // Note: The MyMath class is a proxy class generated by the Wsdl.exe // utility for the Math Web service. This class can also be found in // the SoapHttpClientProtocol class example. Type myType = typeof(MyMath.MyMath); // Get the method info. MethodInfo myBeginMethod = myType.GetMethod("BeginAdd"); MethodInfo myEndMethod = myType.GetMethod("EndAdd"); // Create an instance of the LogicalMethodInfo class. LogicalMethodInfo myLogicalMethodInfo = (LogicalMethodInfo.Create(new MethodInfo[] { myBeginMethod, myEndMethod }, LogicalMethodTypes.Async))[0]; // Get an instance of the proxy class. MyMath.MyMath myMathService = new MyMath.MyMath(); // Call the MyEndIntimationMethod method to intimate the end of // the asynchronous call. AsyncCallback myAsyncCallback = new AsyncCallback(MyEndIntimationMethod); // Begin to invoke the Add method. IAsyncResult myAsyncResult = myLogicalMethodInfo.BeginInvoke( myMathService, new object[] { 10, 10 }, myAsyncCallback, null); // Wait until invoke is complete. myAsyncResult.AsyncWaitHandle.WaitOne(); // Get the result. object[] myReturnValue; myReturnValue = myLogicalMethodInfo.EndInvoke(myMathService, myAsyncResult); Console.WriteLine("Sum of 10 and 10 is " + myReturnValue[0]); }
internal static LogicalMethodInfo[] GetMethods(Type type) { if (type.IsInterface) { throw new InvalidOperationException(Res.GetString(Res.NeedConcreteType, type.FullName)); } ArrayList list = new ArrayList(); MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic); Hashtable unique = new Hashtable(); Hashtable methodInfos = new Hashtable(); for (int i = 0; i < methods.Length; i++) { Type declaringType = methods[i].DeclaringType; if (declaringType == typeof(object)) { continue; } if (declaringType == typeof(WebService)) { continue; } string signature = methods[i].ToString(); MethodInfo declaration = FindInterfaceMethodInfo(declaringType, signature); WebServiceBindingAttribute binding = null; if (declaration != null) { object[] attrs = declaration.DeclaringType.GetCustomAttributes(typeof(WebServiceBindingAttribute), false); if (attrs.Length > 0) { if (attrs.Length > 1) { throw new ArgumentException(Res.GetString(Res.OnlyOneWebServiceBindingAttributeMayBeSpecified1, declaration.DeclaringType.FullName), "type"); } binding = (WebServiceBindingAttribute)attrs[0]; if (binding.Name == null || binding.Name.Length == 0) { binding.Name = declaration.DeclaringType.Name; } } else { declaration = null; } } else if (!methods[i].IsPublic) { continue; } WebMethodAttribute attribute = WebMethodReflector.GetAttribute(methods[i], declaration); if (attribute == null) { continue; } WebMethod webMethod = new WebMethod(declaration, binding, attribute); methodInfos.Add(methods[i], webMethod); MethodInfo method = (MethodInfo)unique[signature]; if (method == null) { unique.Add(signature, methods[i]); list.Add(methods[i]); } else { if (method.DeclaringType.IsAssignableFrom(methods[i].DeclaringType)) { unique[signature] = methods[i]; list[list.IndexOf(method)] = methods[i]; } } } return(LogicalMethodInfo.Create((MethodInfo[])list.ToArray(typeof(MethodInfo)), LogicalMethodTypes.Async | LogicalMethodTypes.Sync, methodInfos)); }
internal static LogicalMethodInfo[] GetMethods(Type type) { if (type.IsInterface) { throw new InvalidOperationException(Res.GetString("NeedConcreteType", new object[] { type.FullName })); } ArrayList list = new ArrayList(); MethodInfo[] methods = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); Hashtable hashtable = new Hashtable(); Hashtable declarations = new Hashtable(); for (int i = 0; i < methods.Length; i++) { Type declaringType = methods[i].DeclaringType; if ((declaringType != typeof(object)) && (declaringType != typeof(WebService))) { string signature = methods[i].ToString(); MethodInfo declaration = FindInterfaceMethodInfo(declaringType, signature); WebServiceBindingAttribute binding = null; if (declaration != null) { object[] customAttributes = declaration.DeclaringType.GetCustomAttributes(typeof(WebServiceBindingAttribute), false); if (customAttributes.Length > 0) { if (customAttributes.Length > 1) { throw new ArgumentException(Res.GetString("OnlyOneWebServiceBindingAttributeMayBeSpecified1", new object[] { declaration.DeclaringType.FullName }), "type"); } binding = (WebServiceBindingAttribute)customAttributes[0]; if ((binding.Name == null) || (binding.Name.Length == 0)) { binding.Name = declaration.DeclaringType.Name; } } else { declaration = null; } } else if (!methods[i].IsPublic) { continue; } WebMethodAttribute attribute = GetAttribute(methods[i], declaration); if (attribute != null) { WebMethod method = new WebMethod(declaration, binding, attribute); declarations.Add(methods[i], method); MethodInfo info2 = (MethodInfo)hashtable[signature]; if (info2 == null) { hashtable.Add(signature, methods[i]); list.Add(methods[i]); } else if (info2.DeclaringType.IsAssignableFrom(methods[i].DeclaringType)) { hashtable[signature] = methods[i]; list[list.IndexOf(info2)] = methods[i]; } } } } return(LogicalMethodInfo.Create((MethodInfo[])list.ToArray(typeof(MethodInfo)), LogicalMethodTypes.Async | LogicalMethodTypes.Sync, declarations)); }