ContractDescriptionGenerator.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. //
  2. // ContractDescriptionGenerator.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005-2007 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Collections.ObjectModel;
  32. using System.Linq;
  33. using System.Net.Security;
  34. using System.Reflection;
  35. using System.Runtime.Serialization;
  36. using System.ServiceModel;
  37. using System.ServiceModel.Channels;
  38. namespace System.ServiceModel.Description
  39. {
  40. internal static class ContractDescriptionGenerator
  41. {
  42. public delegate bool GetOperationContractAttributeExtender (MethodBase method, object[] customAttributes, ref OperationContractAttribute oca);
  43. static List <GetOperationContractAttributeExtender> getOperationContractAttributeExtenders;
  44. public static void RegisterGetOperationContractAttributeExtender (GetOperationContractAttributeExtender extender)
  45. {
  46. if (extender == null)
  47. return;
  48. if (getOperationContractAttributeExtenders == null)
  49. getOperationContractAttributeExtenders = new List <GetOperationContractAttributeExtender> ();
  50. if (getOperationContractAttributeExtenders.Contains (extender))
  51. return;
  52. getOperationContractAttributeExtenders.Add (extender);
  53. }
  54. public static OperationContractAttribute GetOperationContractAttribute (MethodBase method)
  55. {
  56. object [] matts = method.GetCustomAttributes (typeof (OperationContractAttribute), false);
  57. OperationContractAttribute oca;
  58. if (matts.Length == 0)
  59. oca = null;
  60. else
  61. oca = matts [0] as OperationContractAttribute;
  62. if (getOperationContractAttributeExtenders != null && getOperationContractAttributeExtenders.Count > 0) {
  63. foreach (var extender in getOperationContractAttributeExtenders)
  64. if (extender (method, matts, ref oca))
  65. break;
  66. }
  67. return oca;
  68. }
  69. static void GetServiceContractAttribute (Type type, Dictionary<Type,ServiceContractAttribute> table)
  70. {
  71. for (; type != null; type = type.BaseType) {
  72. foreach (ServiceContractAttribute i in
  73. type.GetCustomAttributes (
  74. typeof (ServiceContractAttribute), true))
  75. table [type] = i;
  76. foreach (Type t in type.GetInterfaces ())
  77. GetServiceContractAttribute (t, table);
  78. }
  79. }
  80. public static Dictionary<Type, ServiceContractAttribute> GetServiceContractAttributes (Type type)
  81. {
  82. Dictionary<Type, ServiceContractAttribute> table = new Dictionary<Type, ServiceContractAttribute> ();
  83. GetServiceContractAttribute (type, table);
  84. return table;
  85. }
  86. public static ContractDescription GetContract (
  87. Type contractType) {
  88. return GetContract (contractType, (Type) null);
  89. }
  90. public static ContractDescription GetContract (
  91. Type contractType, object serviceImplementation) {
  92. if (serviceImplementation == null)
  93. throw new ArgumentNullException ("serviceImplementation");
  94. return GetContract (contractType,
  95. serviceImplementation.GetType ());
  96. }
  97. public static MessageContractAttribute GetMessageContractAttribute (Type type)
  98. {
  99. for (Type t = type; t != null; t = t.BaseType) {
  100. object [] matts = t.GetCustomAttributes (
  101. typeof (MessageContractAttribute), true);
  102. if (matts.Length > 0)
  103. return (MessageContractAttribute) matts [0];
  104. }
  105. return null;
  106. }
  107. public static ContractDescription GetCallbackContract (Type serviceType, Type callbackType)
  108. {
  109. return GetContract (callbackType, null, serviceType);
  110. }
  111. public static ContractDescription GetContract (
  112. Type givenContractType, Type givenServiceType)
  113. {
  114. return GetContract (givenContractType, givenServiceType, null);
  115. }
  116. static ContractDescription GetContract (Type givenContractType, Type givenServiceType, Type serviceTypeForCallback)
  117. {
  118. var ret = GetContractInternal (givenContractType, givenServiceType, serviceTypeForCallback);
  119. if (ret == null)
  120. throw new InvalidOperationException (String.Format ("Attempted to get contract type from '{0}' which neither is a service contract nor does it inherit service contract.", serviceTypeForCallback ?? givenContractType));
  121. return ret;
  122. }
  123. internal static ContractDescription GetContractInternal (Type givenContractType, Type givenServiceType, Type serviceTypeForCallback)
  124. {
  125. // FIXME: serviceType should be used for specifying attributes like OperationBehavior.
  126. Type exactContractType = null;
  127. ServiceContractAttribute sca = null;
  128. Dictionary<Type, ServiceContractAttribute> contracts =
  129. GetServiceContractAttributes (serviceTypeForCallback ?? givenServiceType ?? givenContractType);
  130. if (contracts.ContainsKey (givenContractType)) {
  131. exactContractType = givenContractType;
  132. sca = contracts [givenContractType];
  133. } else {
  134. foreach (Type t in contracts.Keys)
  135. if (t.IsAssignableFrom(givenContractType)) {
  136. if (t.IsAssignableFrom (exactContractType)) // exact = IDerived, t = IBase
  137. continue;
  138. if (sca != null && (exactContractType == null || !exactContractType.IsAssignableFrom (t))) // t = IDerived, exact = IBase
  139. throw new InvalidOperationException ("The contract type of " + givenContractType + " is ambiguous: can be either " + exactContractType + " or " + t);
  140. exactContractType = t;
  141. sca = contracts [t];
  142. }
  143. }
  144. if (exactContractType == null)
  145. exactContractType = givenContractType;
  146. if (sca == null) {
  147. if (serviceTypeForCallback != null)
  148. sca = contracts.Values.First ();
  149. else
  150. return null; // no contract
  151. }
  152. string name = sca.Name ?? exactContractType.Name;
  153. string ns = sca.Namespace ?? "http://tempuri.org/";
  154. ContractDescription cd =
  155. new ContractDescription (name, ns);
  156. cd.ContractType = exactContractType;
  157. cd.CallbackContractType = sca.CallbackContract;
  158. cd.SessionMode = sca.SessionMode;
  159. if (sca.ConfigurationName != null)
  160. cd.ConfigurationName = sca.ConfigurationName;
  161. else
  162. cd.ConfigurationName = exactContractType.FullName;
  163. if (sca.HasProtectionLevel)
  164. cd.ProtectionLevel = sca.ProtectionLevel;
  165. foreach (var icd in cd.GetInheritedContracts ()) {
  166. FillOperationsForInterface (icd, icd.ContractType, givenServiceType, false);
  167. foreach (var od in icd.Operations)
  168. cd.Operations.Add (od);
  169. }
  170. FillOperationsForInterface (cd, cd.ContractType, givenServiceType, false);
  171. if (cd.CallbackContractType != null && cd.CallbackContractType != cd.ContractType)
  172. FillOperationsForInterface (cd, cd.CallbackContractType, null, true);
  173. // FIXME: enable this when I found where this check is needed.
  174. /*
  175. if (cd.Operations.Count == 0)
  176. throw new InvalidOperationException (String.Format ("The service contract type {0} has no operation. At least one operation must exist.", contractType));
  177. */
  178. return cd;
  179. }
  180. static void FillOperationsForInterface (ContractDescription cd, Type exactContractType, Type givenServiceType, bool isCallback)
  181. {
  182. // FIXME: load Behaviors
  183. MethodInfo [] contractMethods = /*exactContractType.IsInterface ? GetAllMethods (exactContractType) :*/ exactContractType.GetMethods (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
  184. MethodInfo [] serviceMethods = contractMethods;
  185. if (givenServiceType != null && exactContractType.IsInterface) {
  186. var l = new List<MethodInfo> ();
  187. foreach (Type t in GetAllInterfaceTypes (exactContractType))
  188. l.AddRange (givenServiceType.GetInterfaceMap (t).TargetMethods);
  189. serviceMethods = l.ToArray ();
  190. }
  191. for (int i = 0; i < contractMethods.Length; ++i)
  192. {
  193. MethodInfo mi = contractMethods [i];
  194. OperationContractAttribute oca = GetOperationContractAttribute (mi);
  195. if (oca == null)
  196. continue;
  197. MethodInfo end = null;
  198. if (oca.AsyncPattern) {
  199. if (String.Compare ("Begin", 0, mi.Name,0, 5) != 0)
  200. throw new InvalidOperationException ("For async operation contract patterns, the initiator method name must start with 'Begin'.");
  201. string endName = "End" + mi.Name.Substring (5);
  202. end = mi.DeclaringType.GetMethod (endName);
  203. if (end == null)
  204. throw new InvalidOperationException (String.Format ("'{0}' method is missing. For async operation contract patterns, corresponding End method is required for each Begin method.", endName));
  205. if (GetOperationContractAttribute (end) != null)
  206. throw new InvalidOperationException ("Async 'End' method must not have OperationContractAttribute. It is automatically treated as the EndMethod of the corresponding 'Begin' method.");
  207. }
  208. OperationDescription od = GetOrCreateOperation (cd, mi, serviceMethods [i], oca, end != null ? end.ReturnType : null, isCallback);
  209. if (end != null)
  210. od.EndMethod = end;
  211. }
  212. }
  213. static MethodInfo [] GetAllMethods (Type type)
  214. {
  215. var l = new List<MethodInfo> ();
  216. foreach (var t in GetAllInterfaceTypes (type)) {
  217. #if MONOTOUCH
  218. // The MethodBase[] from t.GetMethods () is cast to a IEnumerable <MethodInfo>
  219. // when passed to List<MethodInfo>.AddRange, which in turn casts it to
  220. // ICollection <MethodInfo>. The full-aot compiler has no idea of this, so
  221. // we're going to make it aware.
  222. int c = ((ICollection <MethodInfo>) t.GetMethods ()).Count;
  223. #endif
  224. l.AddRange (t.GetMethods ());
  225. }
  226. return l.ToArray ();
  227. }
  228. static IEnumerable<Type> GetAllInterfaceTypes (Type type)
  229. {
  230. yield return type;
  231. foreach (var t in type.GetInterfaces ())
  232. foreach (var tt in GetAllInterfaceTypes (t))
  233. yield return tt;
  234. }
  235. static OperationDescription GetOrCreateOperation (
  236. ContractDescription cd, MethodInfo mi, MethodInfo serviceMethod,
  237. OperationContractAttribute oca,
  238. Type asyncReturnType,
  239. bool isCallback)
  240. {
  241. string name = oca.Name ?? (oca.AsyncPattern ? mi.Name.Substring (5) : mi.Name);
  242. OperationDescription od = isCallback ? null : cd.Operations.FirstOrDefault (o => o.Name == name);
  243. if (od == null) {
  244. od = new OperationDescription (name, cd);
  245. od.IsOneWay = oca.IsOneWay;
  246. if (oca.HasProtectionLevel)
  247. od.ProtectionLevel = oca.ProtectionLevel;
  248. if (HasInvalidMessageContract (mi, oca.AsyncPattern))
  249. throw new InvalidOperationException (String.Format ("The operation {0} contains more than one parameters and one or more of them are marked with MessageContractAttribute, but the attribute must be used within an operation that has only one parameter.", od.Name));
  250. od.Messages.Add (GetMessage (od, mi, oca, true, isCallback, null));
  251. if (!od.IsOneWay)
  252. od.Messages.Add (GetMessage (od, mi, oca, false, isCallback, asyncReturnType));
  253. var knownTypeAtts =
  254. cd.ContractType.GetCustomAttributes (typeof (ServiceKnownTypeAttribute), false).Union (
  255. mi.GetCustomAttributes (typeof (ServiceKnownTypeAttribute), false)).Union (
  256. serviceMethod.GetCustomAttributes (typeof (ServiceKnownTypeAttribute), false));
  257. foreach (ServiceKnownTypeAttribute a in knownTypeAtts)
  258. foreach (Type t in a.GetTypes ())
  259. od.KnownTypes.Add (t);
  260. foreach (FaultContractAttribute a in mi.GetCustomAttributes (typeof (FaultContractAttribute), false)) {
  261. var fname = a.Name ?? a.DetailType.Name + "Fault";
  262. var fns = a.Namespace ?? cd.Namespace;
  263. var fd = new FaultDescription (a.Action ?? cd.Namespace + cd.Name + "/" + od.Name + fname) { DetailType = a.DetailType, Name = fname, Namespace = fns };
  264. #if !NET_2_1
  265. if (a.HasProtectionLevel)
  266. fd.ProtectionLevel = a.ProtectionLevel;
  267. #endif
  268. od.Faults.Add (fd);
  269. }
  270. cd.Operations.Add (od);
  271. }
  272. else if ((oca.AsyncPattern && od.BeginMethod != null && od.BeginMethod != mi ||
  273. !oca.AsyncPattern && od.SyncMethod != null && od.SyncMethod != mi) && od.InCallbackContract == isCallback)
  274. throw new InvalidOperationException (String.Format ("contract '{1}' cannot have two operations for '{0}' that have the identical names and different set of parameters.", name, cd.Name));
  275. if (oca.AsyncPattern)
  276. od.BeginMethod = mi;
  277. else
  278. od.SyncMethod = mi;
  279. od.IsInitiating = oca.IsInitiating;
  280. od.IsTerminating = oca.IsTerminating;
  281. if (mi != serviceMethod)
  282. foreach (object obj in mi.GetCustomAttributes (typeof (IOperationBehavior), true))
  283. od.Behaviors.Add ((IOperationBehavior) obj);
  284. if (serviceMethod != null) {
  285. foreach (object obj in serviceMethod.GetCustomAttributes (typeof(IOperationBehavior),true))
  286. od.Behaviors.Add ((IOperationBehavior) obj);
  287. }
  288. #if !NET_2_1
  289. if (od.Behaviors.Find<OperationBehaviorAttribute>() == null)
  290. od.Behaviors.Add (new OperationBehaviorAttribute ());
  291. #endif
  292. // FIXME: fill KnownTypes, Behaviors and Faults.
  293. if (isCallback)
  294. od.InCallbackContract = true;
  295. else
  296. od.InOrdinalContract = true;
  297. return od;
  298. }
  299. static bool HasInvalidMessageContract (MethodInfo mi, bool async)
  300. {
  301. var pars = mi.GetParameters ();
  302. if (async) {
  303. if (pars.Length > 3) {
  304. if (pars.Take (pars.Length - 2).Any (par => par.ParameterType.GetCustomAttribute<MessageContractAttribute> (true) != null))
  305. return true;
  306. }
  307. } else {
  308. if (pars.Length > 1) {
  309. if (pars.Any (par => par.ParameterType.GetCustomAttribute<MessageContractAttribute> (true) != null))
  310. return true;
  311. }
  312. }
  313. return false;
  314. }
  315. static MessageDescription GetMessage (
  316. OperationDescription od, MethodInfo mi,
  317. OperationContractAttribute oca, bool isRequest,
  318. bool isCallback, Type asyncReturnType)
  319. {
  320. ContractDescription cd = od.DeclaringContract;
  321. ParameterInfo [] plist = mi.GetParameters ();
  322. Type messageType = null;
  323. string action = isRequest ? oca.Action : oca.ReplyAction;
  324. MessageContractAttribute mca;
  325. Type retType = asyncReturnType;
  326. if (!isRequest && retType == null)
  327. retType = mi.ReturnType;
  328. // If the argument is only one and has [MessageContract]
  329. // then infer it as a typed messsage
  330. if (isRequest) {
  331. int len = mi.Name.StartsWith ("Begin", StringComparison.Ordinal) ? 3 : 1;
  332. mca = plist.Length != len ? null :
  333. GetMessageContractAttribute (plist [0].ParameterType);
  334. if (mca != null)
  335. messageType = plist [0].ParameterType;
  336. }
  337. else {
  338. mca = GetMessageContractAttribute (retType);
  339. if (mca != null)
  340. messageType = retType;
  341. }
  342. if (action == null)
  343. action = String.Concat (cd.Namespace,
  344. cd.Namespace.Length == 0 ? "urn:" : cd.Namespace.EndsWith ("/") ? "" : "/", cd.Name, "/",
  345. od.Name, isRequest ? String.Empty : "Response");
  346. if (mca != null)
  347. return CreateMessageDescription (messageType, cd.Namespace, action, isRequest, isCallback, mca);
  348. return CreateMessageDescription (oca, plist, od.Name, cd.Namespace, action, isRequest, isCallback, retType, mi.ReturnTypeCustomAttributes);
  349. }
  350. public static MessageDescription CreateMessageDescription (
  351. Type messageType, string defaultNamespace, string action, bool isRequest, bool isCallback, MessageContractAttribute mca)
  352. {
  353. MessageDescription md = new MessageDescription (action, isRequest ^ isCallback ? MessageDirection.Input : MessageDirection.Output) { IsRequest = isRequest };
  354. md.MessageType = MessageFilterOutByRef (messageType);
  355. if (mca.HasProtectionLevel)
  356. md.ProtectionLevel = mca.ProtectionLevel;
  357. MessageBodyDescription mb = md.Body;
  358. if (mca.IsWrapped) {
  359. mb.WrapperName = mca.WrapperName ?? messageType.Name;
  360. mb.WrapperNamespace = mca.WrapperNamespace ?? defaultNamespace;
  361. }
  362. int index = 0;
  363. foreach (MemberInfo bmi in messageType.GetMembers (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
  364. Type mtype = null;
  365. string mname = null;
  366. if (bmi is FieldInfo) {
  367. FieldInfo fi = (FieldInfo) bmi;
  368. mtype = fi.FieldType;
  369. mname = fi.Name;
  370. }
  371. else if (bmi is PropertyInfo) {
  372. PropertyInfo pi = (PropertyInfo) bmi;
  373. mtype = pi.PropertyType;
  374. mname = pi.Name;
  375. }
  376. else
  377. continue;
  378. var mha = bmi.GetCustomAttribute<MessageHeaderAttribute> (false);
  379. if (mha != null) {
  380. var pd = CreateHeaderDescription (mha, mname, defaultNamespace);
  381. pd.Type = MessageFilterOutByRef (mtype);
  382. pd.MemberInfo = bmi;
  383. md.Headers.Add (pd);
  384. }
  385. var mba = GetMessageBodyMemberAttribute (bmi);
  386. if (mba != null) {
  387. var pd = CreatePartCore (mba, mname, defaultNamespace);
  388. if (pd.Index <= 0)
  389. pd.Index = index++;
  390. pd.Type = MessageFilterOutByRef (mtype);
  391. pd.MemberInfo = bmi;
  392. mb.Parts.Add (pd);
  393. }
  394. }
  395. // FIXME: fill headers and properties.
  396. return md;
  397. }
  398. public static MessageDescription CreateMessageDescription (
  399. OperationContractAttribute oca, ParameterInfo[] plist, string name, string defaultNamespace, string action, bool isRequest, bool isCallback, Type retType, ICustomAttributeProvider retTypeAttributes)
  400. {
  401. var dir = isRequest ^ isCallback ? MessageDirection.Input : MessageDirection.Output;
  402. MessageDescription md = new MessageDescription (action, dir) { IsRequest = isRequest };
  403. MessageBodyDescription mb = md.Body;
  404. mb.WrapperName = name + (isRequest ? String.Empty : "Response");
  405. mb.WrapperNamespace = defaultNamespace;
  406. if (oca.HasProtectionLevel)
  407. md.ProtectionLevel = oca.ProtectionLevel;
  408. // Parts
  409. int index = 0;
  410. foreach (ParameterInfo pi in plist) {
  411. // AsyncCallback and state are extraneous.
  412. if (oca.AsyncPattern && pi.Position == plist.Length - 2)
  413. break;
  414. // They are ignored:
  415. // - out parameter in request
  416. // - neither out nor ref parameter in reply
  417. if (isRequest && pi.IsOut)
  418. continue;
  419. if (!isRequest && !pi.IsOut && !pi.ParameterType.IsByRef)
  420. continue;
  421. MessagePartDescription pd = CreatePartCore (GetMessageParameterAttribute (pi), pi.Name, defaultNamespace);
  422. pd.Index = index++;
  423. pd.Type = MessageFilterOutByRef (pi.ParameterType);
  424. mb.Parts.Add (pd);
  425. }
  426. // ReturnValue
  427. if (!isRequest) {
  428. MessagePartDescription mp = CreatePartCore (GetMessageParameterAttribute (retTypeAttributes), name + "Result", mb.WrapperNamespace);
  429. mp.Index = 0;
  430. mp.Type = retType;
  431. mb.ReturnValue = mp;
  432. }
  433. // FIXME: fill properties.
  434. return md;
  435. }
  436. // public static void FillMessageBodyDescriptionByContract (
  437. // Type messageType, MessageBodyDescription mb)
  438. // {
  439. // }
  440. static MessageHeaderDescription CreateHeaderDescription (MessageHeaderAttribute mha, string defaultName, string defaultNamespace)
  441. {
  442. var ret = CreatePartCore<MessageHeaderDescription> (mha, defaultName, defaultNamespace, delegate (string n, string ns) { return new MessageHeaderDescription (n, ns); });
  443. ret.Actor = mha.Actor;
  444. ret.MustUnderstand = mha.MustUnderstand;
  445. ret.Relay = mha.Relay;
  446. return ret;
  447. }
  448. static MessagePartDescription CreatePartCore (
  449. MessageParameterAttribute mpa, string defaultName,
  450. string defaultNamespace)
  451. {
  452. string pname = null;
  453. if (mpa != null && mpa.Name != null)
  454. pname = mpa.Name;
  455. if (pname == null)
  456. pname = defaultName;
  457. return new MessagePartDescription (pname, defaultNamespace);
  458. }
  459. static MessagePartDescription CreatePartCore (MessageBodyMemberAttribute mba, string defaultName, string defaultNamespace)
  460. {
  461. var ret = CreatePartCore<MessagePartDescription> (mba, defaultName, defaultNamespace, delegate (string n, string ns) { return new MessagePartDescription (n, ns); });
  462. ret.Index = mba.Order;
  463. return ret;
  464. }
  465. static T CreatePartCore<T> (MessageContractMemberAttribute mba, string defaultName, string defaultNamespace, Func<string,string,T> creator)
  466. {
  467. string pname = null, pns = null;
  468. if (mba != null) {
  469. if (mba.Name != null)
  470. pname = mba.Name;
  471. if (mba.Namespace != null)
  472. pns = mba.Namespace;
  473. }
  474. if (pname == null)
  475. pname = defaultName;
  476. if (pns == null)
  477. pns = defaultNamespace;
  478. return creator (pname, pns);
  479. }
  480. static Type MessageFilterOutByRef (Type type)
  481. {
  482. return type == null ? null :
  483. type.IsByRef ? type.GetElementType () : type;
  484. }
  485. static MessageParameterAttribute GetMessageParameterAttribute (ICustomAttributeProvider provider)
  486. {
  487. object [] attrs = provider.GetCustomAttributes (
  488. typeof (MessageParameterAttribute), true);
  489. return attrs.Length > 0 ? (MessageParameterAttribute) attrs [0] : null;
  490. }
  491. static MessageBodyMemberAttribute GetMessageBodyMemberAttribute (MemberInfo mi)
  492. {
  493. object [] matts = mi.GetCustomAttributes (
  494. typeof (MessageBodyMemberAttribute), true);
  495. return matts.Length > 0 ? (MessageBodyMemberAttribute) matts [0] : null;
  496. }
  497. }
  498. }