Jypeli 10
The simple game programming library
ParseMethodAttribute.cs
Siirry tämän tiedoston dokumentaatioon.
1#region MIT License
2/*
3 * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22#endregion
23
24
25
26using System;
27using System.Reflection;
28
29namespace AdvanceMath.Design
30{
31 [global::System.AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
32 public sealed class ParseMethodAttribute : Attribute
33 {
34#if !WINDOWS_STOREAPP
35 public static MethodInfo GetParseMethod(Type t)
36 {
37 foreach (MethodInfo method in t.GetMethods())
38 {
39 if (method.IsStatic &&
40 method.GetCustomAttributes(typeof(ParseMethodAttribute), true).Length > 0 &&
41 method.GetParameters().Length == 1 &&
42 method.GetParameters()[0].ParameterType == typeof(string) &&
43 method.ReturnType == t)
44 {
45 return method;
46 }
47 }
48 return null;
49 }
50#else
51 public static MethodInfo GetParseMethod(Type t)
52 {
53 // Use the Win8 "new" reflection model
54 TypeInfo ti = t.GetTypeInfo();
55
56 foreach (MethodInfo method in ti.DeclaredMethods)
57 {
58 if (method.IsStatic &&
59 !IsEmpty( method.GetCustomAttributes(typeof(ParseMethodAttribute), true) ) &&
60 method.GetParameters().Length == 1 &&
61 method.GetParameters()[0].ParameterType == typeof(string) &&
62 method.ReturnType == t)
63 {
64 return method;
65 }
66 }
67 return null;
68 }
69
70 private static bool IsEmpty( IEnumerable collection )
71 {
72 IEnumerator e = collection.GetEnumerator();
73 return e.MoveNext();
74 }
75#endif
76 }
77}