private static bool HasCast(Type type, Type from, Type to, out MethodInfo op) { #if WINRT System.Collections.Generic.List<MethodInfo> list = new System.Collections.Generic.List<MethodInfo>(); foreach (var item in type.GetRuntimeMethods()) { if (item.IsStatic) list.Add(item); } MethodInfo[] found = list.ToArray(); #else const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; MethodInfo[] found = type.GetMethods(flags); #endif for(int i = 0 ; i < found.Length ; i++) { MethodInfo m = found[i]; if ((m.Name != "op_Implicit" && m.Name != "op_Explicit") || m.ReturnType != to) { continue; } ParameterInfo[] paramTypes = m.GetParameters(); if(paramTypes.Length == 1 && paramTypes[0].ParameterType == from) { op = m; return true; } } op = null; return false; }
private static bool HasCast(Type type, Type from, Type to, out MethodInfo op) { #if WINRT System.Collections.Generic.List <MethodInfo> list = new System.Collections.Generic.List <MethodInfo>(); foreach (var item in type.GetRuntimeMethods()) { if (item.IsStatic) { list.Add(item); } } MethodInfo[] found = list.ToArray(); #else const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; MethodInfo[] found = type.GetMethods(flags); #endif for (int i = 0; i < found.Length; i++) { MethodInfo m = found[i]; if ((m.Name != "op_Implicit" && m.Name != "op_Explicit") || m.ReturnType != to) { continue; } ParameterInfo[] paramTypes = m.GetParameters(); if (paramTypes.Length == 1 && paramTypes[0].ParameterType == from) { op = m; return(true); } } op = null; return(false); }
private static bool HasCast(TypeModel model, Type type, Type from, Type to, out MethodInfo op) { #if WINRT System.Collections.Generic.List<MethodInfo> list = new System.Collections.Generic.List<MethodInfo>(); foreach (var item in type.GetRuntimeMethods()) { if (item.IsStatic) list.Add(item); } MethodInfo[] found = list.ToArray(); #else const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; MethodInfo[] found = type.GetMethods(flags); #endif ParameterInfo[] paramTypes; Type convertAttributeType = null; for (int i = 0; i < found.Length; i++) { MethodInfo m = found[i]; if (m.ReturnType != to) continue; paramTypes = m.GetParameters(); if(paramTypes.Length == 1 && paramTypes[0].ParameterType == from) { if (convertAttributeType == null) { convertAttributeType = model.MapType(typeof(ProtoConverterAttribute), false); if (convertAttributeType == null) { // attribute isn't defined in the source assembly: stop looking break; } } if (m.IsDefined(convertAttributeType, true)) { op = m; return true; } } } for(int i = 0 ; i < found.Length ; i++) { MethodInfo m = found[i]; if ((m.Name != "op_Implicit" && m.Name != "op_Explicit") || m.ReturnType != to) { continue; } paramTypes = m.GetParameters(); if(paramTypes.Length == 1 && paramTypes[0].ParameterType == from) { op = m; return true; } } op = null; return false; }
private static bool HasCast(TypeModel model, Type type, Type from, Type to, out MethodInfo op) { #if WINRT System.Collections.Generic.List <MethodInfo> list = new System.Collections.Generic.List <MethodInfo>(); foreach (var item in type.GetRuntimeMethods()) { if (item.IsStatic) { list.Add(item); } } MethodInfo[] found = list.ToArray(); #else const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; MethodInfo[] found = type.GetMethods(flags); #endif ParameterInfo[] paramTypes; for (int i = 0; i < found.Length; i++) { MethodInfo m = found[i]; if (m.ReturnType != to) { continue; } paramTypes = m.GetParameters(); if (paramTypes.Length == 1 && paramTypes[0].ParameterType == from) { if (AttributeMap.GetAttribute(AttributeMap.Create(model, m, false), "ProtoBuf.ProtoConverterAttribute") != null || AttributeMap.GetAttribute(AttributeMap.Create(model, m, false), "AqlaSerializer.SurrogateConverterAttribute") != null) { op = m; return(true); } } } for (int i = 0; i < found.Length; i++) { MethodInfo m = found[i]; if ((m.Name != "op_Implicit" && m.Name != "op_Explicit") || m.ReturnType != to) { continue; } paramTypes = m.GetParameters(); if (paramTypes.Length == 1 && paramTypes[0].ParameterType == from) { op = m; return(true); } } op = null; return(false); }
internal static MethodInfo GetStaticMethod(Type declaringType, string name) { var methods = declaringType.GetRuntimeMethods(); foreach (MethodInfo method in methods) { if (method.Name == name) { return(method); } } return(null); }
internal static MethodInfo GetInstanceMethod(Type declaringType, string name, Type[] parameterTypes) { var methods = declaringType.GetRuntimeMethods(); foreach (MethodInfo method in methods) { if (method.Name == name && IsMatch(method.GetParameters(), parameterTypes)) { return(method); } } return(null); }
private static bool HasCast(TypeModel model, Type type, Type from, Type to, out MethodInfo op) { #if WINRT System.Collections.Generic.List <MethodInfo> list = new System.Collections.Generic.List <MethodInfo>(); foreach (var item in type.GetRuntimeMethods()) { if (item.IsStatic) { list.Add(item); } } MethodInfo[] found = list.ToArray(); #else const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; MethodInfo[] found = type.GetMethods(flags); #endif ParameterInfo[] paramTypes; Type convertAttributeType = null; for (int i = 0; i < found.Length; i++) { MethodInfo m = found[i]; if (m.ReturnType != to) { continue; } paramTypes = m.GetParameters(); if (paramTypes.Length == 1 && paramTypes[0].ParameterType == from) { if (convertAttributeType == null) { convertAttributeType = model.MapType(typeof(ProtoConverterAttribute), false); if (convertAttributeType == null) { // attribute isn't defined in the source assembly: stop looking break; } } if (m.IsDefined(convertAttributeType, true)) { op = m; return(true); } } } for (int i = 0; i < found.Length; i++) { MethodInfo m = found[i]; if ((m.Name != "op_Implicit" && m.Name != "op_Explicit") || m.ReturnType != to) { continue; } paramTypes = m.GetParameters(); if (paramTypes.Length == 1 && paramTypes[0].ParameterType == from) { op = m; return(true); } } op = null; return(false); }
internal static Type GetListItemType(TypeModel model, Type listType) { Helpers.DebugAssert(listType != null); #if WINRT TypeInfo listTypeInfo = listType.GetTypeInfo(); if (listType == typeof(string) || listType.IsArray || !typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(listTypeInfo)) { return(null); } #else if (listType == model.MapType(typeof(string)) || listType.IsArray || !model.MapType(typeof(IEnumerable)).IsAssignableFrom(listType)) { return(null); } #endif BasicList candidates = new BasicList(); #if WINRT foreach (MethodInfo method in listType.GetRuntimeMethods()) #else foreach (MethodInfo method in listType.GetMethods()) #endif { if (method.IsStatic || method.Name != "Add") { continue; } ParameterInfo[] parameters = method.GetParameters(); Type paramType; if (parameters.Length == 1 && !candidates.Contains(paramType = parameters[0].ParameterType)) { candidates.Add(paramType); } } string name = listType.Name; bool isQueueStack = name != null && (name.IndexOf("Queue", System.StringComparison.Ordinal) >= 0 || name.IndexOf("Stack", System.StringComparison.Ordinal) >= 0); #if !NO_GENERICS if (!isQueueStack) { TestEnumerableListPatterns(model, candidates, listType); #if WINRT foreach (Type iType in listTypeInfo.ImplementedInterfaces) { TestEnumerableListPatterns(model, candidates, iType); } #else foreach (Type iType in listType.GetInterfaces()) { TestEnumerableListPatterns(model, candidates, iType); } #endif } #endif #if WINRT // more convenient GetProperty overload not supported on all platforms foreach (PropertyInfo indexer in listType.GetRuntimeProperties()) { if (indexer.Name != "Item" || candidates.Contains(indexer.PropertyType)) { continue; } ParameterInfo[] args = indexer.GetIndexParameters(); if (args.Length != 1 || args[0].ParameterType != typeof(int)) { continue; } MethodInfo getter = indexer.GetMethod; if (getter == null || getter.IsStatic) { continue; } candidates.Add(indexer.PropertyType); } #else // more convenient GetProperty overload not supported on all platforms foreach (PropertyInfo indexer in listType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { if (indexer.Name != "Item" || candidates.Contains(indexer.PropertyType)) { continue; } ParameterInfo[] args = indexer.GetIndexParameters(); if (args.Length != 1 || args[0].ParameterType != model.MapType(typeof(int))) { continue; } candidates.Add(indexer.PropertyType); } #endif switch (candidates.Count) { case 0: return(null); case 1: return((Type)candidates[0]); case 2: if (CheckDictionaryAccessors(model, (Type)candidates[0], (Type)candidates[1])) { return((Type)candidates[0]); } if (CheckDictionaryAccessors(model, (Type)candidates[1], (Type)candidates[0])) { return((Type)candidates[1]); } break; } return(null); }