ContractDescriptionGenerator.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 (Type contractType) {
  87. return GetContract (contractType, (Type) null);
  88. }
  89. public static ContractDescription GetContract (
  90. Type contractType, object serviceImplementation) {
  91. if (serviceImplementation == null)
  92. throw new ArgumentNullException ("serviceImplementation");
  93. return GetContract (contractType,
  94. serviceImplementation.GetType ());
  95. }
  96. public static MessageContractAttribute GetMessageContractAttribute (Type type)
  97. {
  98. for (Type t = type; t != null; t = t.BaseType) {
  99. object [] matts = t.GetCustomAttributes (
  100. typeof (MessageContractAttribute), true);
  101. if (matts.Length > 0)
  102. return (MessageContractAttribute) matts [0];
  103. }
  104. return null;
  105. }
  106. public static ContractDescription GetCallbackContract (Type serviceType, Type callbackType)
  107. {
  108. return GetContract (callbackType, null, serviceType);
  109. }
  110. public static ContractDescription GetContract (
  111. Type givenContractType, Type givenServiceType)
  112. {
  113. return GetContract (givenContractType, givenServiceType, null);
  114. }
  115. static ContractDescription GetContract (Type givenContractType, Type givenServiceType, Type serviceTypeForCallback)
  116. {
  117. var ret = GetContractInternal (givenContractType, givenServiceType, serviceTypeForCallback);
  118. if (ret == null)
  119. 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));
  120. return ret;
  121. }
  122. internal static ContractDescription GetContractInternal (Type givenContractType, Type givenServiceType, Type serviceTypeForCallback)
  123. {
  124. if (givenContractType == null)
  125. throw new ArgumentNullException ("givenContractType");
  126. // FIXME: serviceType should be used for specifying attributes like OperationBehavior.
  127. Type exactContractType = null;
  128. ServiceContractAttribute sca = null;
  129. Dictionary<Type, ServiceContractAttribute> contracts =
  130. GetServiceContractAttributes (serviceTypeForCallback ?? givenServiceType ?? givenContractType);
  131. if (contracts.ContainsKey (givenContractType)) {
  132. exactContractType = givenContractType;
  133. sca = contracts [givenContractType];
  134. } else {
  135. foreach (Type t in contracts.Keys)
  136. if (t.IsAssignableFrom(givenContractType)) {
  137. if (t.IsAssignableFrom (exactContractType)) // exact = IDerived, t = IBase
  138. continue;
  139. if (sca != null && (exactContractType == null || !exactContractType.IsAssignableFrom (t))) // t = IDerived, exact = IBase
  140. throw new InvalidOperationException ("The contract type of " + givenContractType + " is ambiguous: can be either " + exactContractType + " or " + t);
  141. exactContractType = t;
  142. sca = contracts [t];
  143. }
  144. }
  145. if (exactContractType == null)
  146. exactContractType = givenContractType;
  147. if (sca == null) {
  148. if (serviceTypeForCallback != null)
  149. sca = contracts.Values.First ();
  150. else
  151. return null; // no contract
  152. }
  153. string name = sca.Name ?? exactContractType.Name;
  154. string ns = sca.Namespace ?? "http://tempuri.org/";
  155. ContractDescription cd =
  156. new ContractDescription (name, ns);
  157. cd.ContractType = exactContractType;
  158. cd.CallbackContractType = sca.CallbackContract;
  159. cd.SessionMode = sca.SessionMode;
  160. if (sca.ConfigurationName != null)
  161. cd.ConfigurationName = sca.ConfigurationName;
  162. else
  163. cd.ConfigurationName = exactContractType.FullName;
  164. if (sca.HasProtectionLevel)
  165. cd.ProtectionLevel = sca.ProtectionLevel;
  166. foreach (var icd in cd.GetInheritedContracts ()) {
  167. FillOperationsForInterface (icd, icd.ContractType, givenServiceType, false);
  168. foreach (var od in icd.Operations)
  169. if (!cd.Operations.Any(o => o.Name == od.Name && o.SyncMethod == od.SyncMethod &&
  170. o.BeginMethod == od.BeginMethod && o.InCallbackContract == od.InCallbackContract))
  171. cd.Operations.Add (od);
  172. }
  173. FillOperationsForInterface (cd, cd.ContractType, givenServiceType, false);
  174. if (cd.CallbackContractType != null)
  175. FillOperationsForInterface (cd, cd.CallbackContractType, null, true);
  176. // FIXME: enable this when I found where this check is needed.
  177. /*
  178. if (cd.Operations.Count == 0)
  179. throw new InvalidOperationException (String.Format ("The service contract type {0} has no operation. At least one operation must exist.", contractType));
  180. */
  181. return cd;
  182. }
  183. static void FillOperationsForInterface (ContractDescription cd, Type exactContractType, Type givenServiceType, bool isCallback)
  184. {
  185. // FIXME: load Behaviors
  186. MethodInfo [] contractMethods = /*exactContractType.IsInterface ? GetAllMethods (exactContractType) :*/ exactContractType.GetMethods (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
  187. MethodInfo [] serviceMethods = contractMethods;
  188. if (givenServiceType != null && exactContractType.IsInterface) {
  189. var l = new List<MethodInfo> ();
  190. foreach (Type t in GetAllInterfaceTypes (exactContractType))
  191. l.AddRange (givenServiceType.GetInterfaceMap (t).TargetMethods);
  192. serviceMethods = l.ToArray ();
  193. }
  194. for (int i = 0; i < contractMethods.Length; ++i)
  195. {
  196. MethodInfo mi = contractMethods [i];
  197. OperationContractAttribute oca = GetOperationContractAttribute (mi);
  198. if (oca == null)
  199. continue;
  200. MethodInfo end = null;
  201. if (oca.AsyncPattern) {
  202. if (String.Compare ("Begin", 0, mi.Name,0, 5) != 0)
  203. throw new InvalidOperationException ("For async operation contract patterns, the initiator method name must start with 'Begin'.");
  204. string endName = "End" + mi.Name.Substring (5);
  205. end = mi.DeclaringType.GetMethod (endName);
  206. if (end == null)
  207. throw new InvalidOperationException (String.Format ("'{0}' method is missing. For async operation contract patterns, corresponding End method is required for each Begin method.", endName));
  208. if (GetOperationContractAttribute (end) != null)
  209. throw new InvalidOperationException ("Async 'End' method must not have OperationContractAttribute. It is automatically treated as the EndMethod of the corresponding 'Begin' method.");
  210. }
  211. OperationDescription od = GetOrCreateOperation (cd, mi, serviceMethods [i], oca, end != null ? end.ReturnType : null, isCallback);
  212. if (end != null)
  213. od.EndMethod = end;
  214. }
  215. }
  216. static MethodInfo [] GetAllMethods (Type type)
  217. {
  218. var l = new List<MethodInfo> ();
  219. foreach (var t in GetAllInterfaceTypes (type)) {
  220. #if MONOTOUCH
  221. // The MethodBase[] from t.GetMethods () is cast to a IEnumerable <MethodInfo>
  222. // when passed to List<MethodInfo>.AddRange, which in turn casts it to
  223. // ICollection <MethodInfo>. The full-aot compiler has no idea of this, so
  224. // we're going to make it aware.
  225. int c = ((ICollection <MethodInfo>) t.GetMethods ()).Count;
  226. #endif
  227. l.AddRange (t.GetMethods ());
  228. }
  229. return l.ToArray ();
  230. }
  231. static IEnumerable<Type> GetAllInterfaceTypes (Type type)
  232. {
  233. yield return type;
  234. foreach (var t in type.GetInterfaces ())
  235. foreach (var tt in GetAllInterfaceTypes (t))
  236. yield return tt;
  237. }
  238. static OperationDescription GetOrCreateOperation (
  239. ContractDescription cd, MethodInfo mi, MethodInfo serviceMethod,
  240. OperationContractAttribute oca,
  241. Type asyncReturnType,
  242. bool isCallback)
  243. {
  244. string name = oca.Name ?? (oca.AsyncPattern ? mi.Name.Substring (5) : mi.Name);
  245. OperationDescription od = cd.Operations.FirstOrDefault (o => o.Name == name && o.InCallbackContract == isCallback);
  246. if (od == null) {
  247. od = new OperationDescription (name, cd);
  248. od.IsOneWay = oca.IsOneWay;
  249. if (oca.HasProtectionLevel)
  250. od.ProtectionLevel = oca.ProtectionLevel;
  251. if (HasInvalidMessageContract (mi, oca.AsyncPattern))
  252. 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));
  253. #if !NET_2_1
  254. var xfa = serviceMethod.GetCustomAttribute<XmlSerializerFormatAttribute> (false);
  255. if (xfa != null)
  256. od.Behaviors.Add (new XmlSerializerOperationBehavior (od, xfa));
  257. #endif
  258. var dfa = serviceMethod.GetCustomAttribute<DataContractFormatAttribute> (false);
  259. if (dfa != null)
  260. od.Behaviors.Add (new DataContractSerializerOperationBehavior (od, dfa));
  261. od.Messages.Add (GetMessage (od, mi, oca, true, isCallback, null));
  262. if (!od.IsOneWay)
  263. od.Messages.Add (GetMessage (od, mi, oca, false, isCallback, asyncReturnType));
  264. var knownTypeAtts =
  265. cd.ContractType.GetCustomAttributes (typeof (ServiceKnownTypeAttribute), false).Union (
  266. mi.GetCustomAttributes (typeof (ServiceKnownTypeAttribute), false)).Union (
  267. serviceMethod.GetCustomAttributes (typeof (ServiceKnownTypeAttribute), false));
  268. foreach (ServiceKnownTypeAttribute a in knownTypeAtts)
  269. foreach (Type t in a.GetTypes ())
  270. od.KnownTypes.Add (t);
  271. foreach (FaultContractAttribute a in mi.GetCustomAttributes (typeof (FaultContractAttribute), false)) {
  272. var fname = a.Name ?? a.DetailType.Name + "Fault";
  273. var fns = a.Namespace ?? cd.Namespace;
  274. var fd = new FaultDescription (a.Action ?? cd.Namespace + cd.Name + "/" + od.Name + fname) { DetailType = a.DetailType, Name = fname, Namespace = fns };
  275. #if !NET_2_1
  276. if (a.HasProtectionLevel)
  277. fd.ProtectionLevel = a.ProtectionLevel;
  278. #endif
  279. od.Faults.Add (fd);
  280. }
  281. cd.Operations.Add (od);
  282. }
  283. else if ((oca.AsyncPattern && od.BeginMethod != null && od.BeginMethod != mi ||
  284. !oca.AsyncPattern && od.SyncMethod != null && od.SyncMethod != mi) && od.InCallbackContract == isCallback)
  285. 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));
  286. if (oca.AsyncPattern)
  287. od.BeginMethod = mi;
  288. else
  289. od.SyncMethod = mi;
  290. od.IsInitiating = oca.IsInitiating;
  291. od.IsTerminating = oca.IsTerminating;
  292. if (mi != serviceMethod)
  293. foreach (object obj in mi.GetCustomAttributes (typeof (IOperationBehavior), true))
  294. od.Behaviors.Add ((IOperationBehavior) obj);
  295. if (serviceMethod != null) {
  296. foreach (object obj in serviceMethod.GetCustomAttributes (typeof(IOperationBehavior),true))
  297. od.Behaviors.Add ((IOperationBehavior) obj);
  298. }
  299. #if !NET_2_1
  300. if (od.Behaviors.Find<OperationBehaviorAttribute>() == null)
  301. od.Behaviors.Add (new OperationBehaviorAttribute ());
  302. #endif
  303. // FIXME: fill KnownTypes, Behaviors and Faults.
  304. if (isCallback)
  305. od.InCallbackContract = true;
  306. else
  307. od.InOrdinalContract = true;
  308. return od;
  309. }
  310. static bool HasInvalidMessageContract (MethodInfo mi, bool async)
  311. {
  312. var pars = mi.GetParameters ();
  313. if (async) {
  314. if (pars.Length > 3) {
  315. if (pars.Take (pars.Length - 2).Any (par => par.ParameterType.GetCustomAttribute<MessageContractAttribute> (true) != null))
  316. return true;
  317. }
  318. } else {
  319. if (pars.Length > 1) {
  320. if (pars.Any (par => par.ParameterType.GetCustomAttribute<MessageContractAttribute> (true) != null))
  321. return true;
  322. }
  323. }
  324. return false;
  325. }
  326. static MessageDescription GetMessage (
  327. OperationDescription od, MethodInfo mi,
  328. OperationContractAttribute oca, bool isRequest,
  329. bool isCallback, Type asyncReturnType)
  330. {
  331. ContractDescription cd = od.DeclaringContract;
  332. ParameterInfo [] plist = mi.GetParameters ();
  333. Type messageType = null;
  334. string action = isRequest ? oca.Action : oca.ReplyAction;
  335. MessageContractAttribute mca;
  336. Type retType = asyncReturnType;
  337. if (!isRequest && retType == null)
  338. retType = mi.ReturnType;
  339. // If the argument is only one and has [MessageContract]
  340. // then infer it as a typed messsage
  341. if (isRequest) {
  342. int len = mi.Name.StartsWith ("Begin", StringComparison.Ordinal) ? 3 : 1;
  343. mca = plist.Length != len ? null :
  344. GetMessageContractAttribute (plist [0].ParameterType);
  345. if (mca != null)
  346. messageType = plist [0].ParameterType;
  347. }
  348. else {
  349. mca = GetMessageContractAttribute (retType);
  350. if (mca != null)
  351. messageType = retType;
  352. }
  353. if (action == null)
  354. action = String.Concat (cd.Namespace,
  355. cd.Namespace.Length == 0 ? "urn:" : cd.Namespace.EndsWith ("/") ? "" : "/", cd.Name, "/",
  356. od.Name, isRequest ? String.Empty : "Response");
  357. if (mca != null)
  358. return CreateMessageDescription (messageType, cd.Namespace, action, isRequest, isCallback, mca);
  359. return CreateMessageDescription (oca, plist, od.Name, cd.Namespace, action, isRequest, isCallback, retType, mi.ReturnTypeCustomAttributes);
  360. }
  361. public static MessageDescription CreateMessageDescription (
  362. Type messageType, string defaultNamespace, string action, bool isRequest, bool isCallback, MessageContractAttribute mca)
  363. {
  364. MessageDescription md = new MessageDescription (action, isRequest ^ isCallback ? MessageDirection.Input : MessageDirection.Output) { IsRequest = isRequest };
  365. md.MessageType = MessageFilterOutByRef (messageType);
  366. if (mca.HasProtectionLevel)
  367. md.ProtectionLevel = mca.ProtectionLevel;
  368. MessageBodyDescription mb = md.Body;
  369. if (mca.IsWrapped) {
  370. mb.WrapperName = mca.WrapperName ?? messageType.Name;
  371. mb.WrapperNamespace = mca.WrapperNamespace ?? defaultNamespace;
  372. }
  373. int index = 0;
  374. foreach (MemberInfo bmi in messageType.GetMembers (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
  375. Type mtype = null;
  376. string mname = null;
  377. if (bmi is FieldInfo) {
  378. FieldInfo fi = (FieldInfo) bmi;
  379. mtype = fi.FieldType;
  380. mname = fi.Name;
  381. }
  382. else if (bmi is PropertyInfo) {
  383. PropertyInfo pi = (PropertyInfo) bmi;
  384. mtype = pi.PropertyType;
  385. mname = pi.Name;
  386. }
  387. else
  388. continue;
  389. var mha = bmi.GetCustomAttribute<MessageHeaderAttribute> (false);
  390. if (mha != null) {
  391. var pd = CreateHeaderDescription (mha, mname, defaultNamespace);
  392. pd.Type = MessageFilterOutByRef (mtype);
  393. pd.MemberInfo = bmi;
  394. md.Headers.Add (pd);
  395. }
  396. var mba = GetMessageBodyMemberAttribute (bmi);
  397. if (mba != null) {
  398. var pd = CreatePartCore (mba, mname, defaultNamespace);
  399. if (pd.Index <= 0)
  400. pd.Index = index++;
  401. pd.Type = MessageFilterOutByRef (mtype);
  402. pd.MemberInfo = bmi;
  403. mb.Parts.Add (pd);
  404. }
  405. }
  406. // FIXME: fill headers and properties.
  407. return md;
  408. }
  409. public static MessageDescription CreateMessageDescription (
  410. OperationContractAttribute oca, ParameterInfo[] plist, string name, string defaultNamespace, string action, bool isRequest, bool isCallback, Type retType, ICustomAttributeProvider retTypeAttributes)
  411. {
  412. var dir = isRequest ^ isCallback ? MessageDirection.Input : MessageDirection.Output;
  413. MessageDescription md = new MessageDescription (action, dir) { IsRequest = isRequest };
  414. MessageBodyDescription mb = md.Body;
  415. mb.WrapperName = name + (isRequest ? String.Empty : "Response");
  416. mb.WrapperNamespace = defaultNamespace;
  417. if (oca.HasProtectionLevel)
  418. md.ProtectionLevel = oca.ProtectionLevel;
  419. // Parts
  420. int index = 0;
  421. foreach (ParameterInfo pi in plist) {
  422. // AsyncCallback and state are extraneous.
  423. if (oca.AsyncPattern && pi.Position == plist.Length - 2)
  424. break;
  425. // They are ignored:
  426. // - out parameter in request
  427. // - neither out nor ref parameter in reply
  428. if (isRequest && pi.IsOut)
  429. continue;
  430. if (!isRequest && !pi.IsOut && !pi.ParameterType.IsByRef)
  431. continue;
  432. MessagePartDescription pd = CreatePartCore (GetMessageParameterAttribute (pi), pi.Name, defaultNamespace);
  433. pd.Index = index++;
  434. pd.Type = MessageFilterOutByRef (pi.ParameterType);
  435. mb.Parts.Add (pd);
  436. }
  437. // ReturnValue
  438. if (!isRequest) {
  439. MessagePartDescription mp = CreatePartCore (GetMessageParameterAttribute (retTypeAttributes), name + "Result", mb.WrapperNamespace);
  440. mp.Index = 0;
  441. mp.Type = retType;
  442. mb.ReturnValue = mp;
  443. }
  444. // FIXME: fill properties.
  445. return md;
  446. }
  447. // public static void FillMessageBodyDescriptionByContract (
  448. // Type messageType, MessageBodyDescription mb)
  449. // {
  450. // }
  451. static MessageHeaderDescription CreateHeaderDescription (MessageHeaderAttribute mha, string defaultName, string defaultNamespace)
  452. {
  453. var ret = CreatePartCore<MessageHeaderDescription> (mha, defaultName, defaultNamespace, delegate (string n, string ns) { return new MessageHeaderDescription (n, ns); });
  454. ret.Actor = mha.Actor;
  455. ret.MustUnderstand = mha.MustUnderstand;
  456. ret.Relay = mha.Relay;
  457. return ret;
  458. }
  459. static MessagePartDescription CreatePartCore (
  460. MessageParameterAttribute mpa, string defaultName,
  461. string defaultNamespace)
  462. {
  463. string pname = null;
  464. if (mpa != null && mpa.Name != null)
  465. pname = mpa.Name;
  466. if (pname == null)
  467. pname = defaultName;
  468. return new MessagePartDescription (pname, defaultNamespace);
  469. }
  470. static MessagePartDescription CreatePartCore (MessageBodyMemberAttribute mba, string defaultName, string defaultNamespace)
  471. {
  472. var ret = CreatePartCore<MessagePartDescription> (mba, defaultName, defaultNamespace, delegate (string n, string ns) { return new MessagePartDescription (n, ns); });
  473. ret.Index = mba.Order;
  474. return ret;
  475. }
  476. static T CreatePartCore<T> (MessageContractMemberAttribute mba, string defaultName, string defaultNamespace, Func<string,string,T> creator)
  477. {
  478. string pname = null, pns = null;
  479. if (mba != null) {
  480. if (mba.Name != null)
  481. pname = mba.Name;
  482. if (mba.Namespace != null)
  483. pns = mba.Namespace;
  484. }
  485. if (pname == null)
  486. pname = defaultName;
  487. if (pns == null)
  488. pns = defaultNamespace;
  489. return creator (pname, pns);
  490. }
  491. static Type MessageFilterOutByRef (Type type)
  492. {
  493. return type == null ? null :
  494. type.IsByRef ? type.GetElementType () : type;
  495. }
  496. static MessageParameterAttribute GetMessageParameterAttribute (ICustomAttributeProvider provider)
  497. {
  498. object [] attrs = provider.GetCustomAttributes (
  499. typeof (MessageParameterAttribute), true);
  500. return attrs.Length > 0 ? (MessageParameterAttribute) attrs [0] : null;
  501. }
  502. static MessageBodyMemberAttribute GetMessageBodyMemberAttribute (MemberInfo mi)
  503. {
  504. object [] matts = mi.GetCustomAttributes (
  505. typeof (MessageBodyMemberAttribute), true);
  506. return matts.Length > 0 ? (MessageBodyMemberAttribute) matts [0] : null;
  507. }
  508. }
  509. }