public static NativeMethodDescription GetNativeMethodFrom(ProgrammingPlatform programmingPlatform, MethodInfo methodInfo, NativeCallableAttribute nativeAttribute) { // Passed through GetNativeType twice, once for hte native typecoversion in C# and another for the destination type (e.g. ruby, python) return(new NativeMethodDescription { Name = nativeAttribute.EntryPoint, ReturnType = NativeTypeDescription.FromString(GetNativeType(programmingPlatform, GetNativeType(methodInfo.ReturnType.FullName))), IntendedReturnType = NativeTypeDescription.FromString(GetNativeType(programmingPlatform, GetNativeType(methodInfo.ReturnTypeCustomAttributes.GetCustomAttributes(true).Where(c => c is IntendedTypeAttribute).Select(c => c as IntendedTypeAttribute).FirstOrDefault()?.Type.FullName))), Parameters = GetNativeParametersFrom(programmingPlatform, methodInfo) }); }
public static List <NativeParameterDescription> GetNativeParametersFrom(ProgrammingPlatform programmingPlatform, MethodInfo methodInfo) { var parameters = new List <NativeParameterDescription> (); foreach (var parameter in methodInfo.GetParameters()) { // Passed through GetNativeType twice, once for hte native typecoversion in C# and another for the destination type (e.g. ruby, python) parameters.Add(new NativeParameterDescription(parameter.Name, NativeTypeDescription.FromString(GetNativeType(programmingPlatform, GetNativeType(parameter.ParameterType.FullName))), NativeTypeDescription.FromString(GetNativeType(programmingPlatform, GetNativeType(parameter.GetCustomAttribute <IntendedTypeAttribute> (true)?.Type.FullName))))); } return(parameters); }
public static string?GetNativeType(ProgrammingPlatform programmingPlatform, string?typeName) { if (typeName == null) { return(null); } switch (programmingPlatform) { case ProgrammingPlatform.dotnet: { // .NET Case switch (typeName) { case var t when t.CompareTo("System.Int32") == 0 || t.CompareTo("int") == 0: return("int"); case var t when t.CompareTo("pointer") == 0 || t.CompareTo("System.IntPtr") == 0: return("pointer"); case var t when t.CompareTo("string") == 0 || t.CompareTo("System.String") == 0: return("string"); } break; } case ProgrammingPlatform.ruby: { // .NET Case switch (typeName.ToLower()) { case var t when t.CompareTo("int") == 0: return("int"); case var t when t.CompareTo("pointer") == 0: return("pointer"); case var t when t.CompareTo("string") == 0: return("string"); default: return("void"); } } } return("none"); }
public static Dictionary <string, NativeNamespaceDescription> ProcessAssembly(Assembly assembly, ProgrammingPlatform progammingPlaformNumber) { //var methodsAndAttributes = from methodInfo in typeof(Library).GetMethods(BindingFlags.Static | BindingFlags.Public) let nativeAttribute = methodInfo.GetCustomAttribute(typeof(NativeCallableAttribute), true) as NativeCallableAttribute where nativeAttribute != null select GetNativeMethodFrom(methodInfo, nativeAttribute); var namespaces = new Dictionary <string, NativeNamespaceDescription> (); foreach (var classFound in assembly.GetTypes()) { foreach (var methodInfo in classFound.GetMethods(BindingFlags.Static | BindingFlags.Public)) { var nativeAttribute = methodInfo.GetCustomAttribute(typeof(NativeCallableAttribute), true) as NativeCallableAttribute; if (nativeAttribute == null) { continue; } var currentMethodName = nativeAttribute.EntryPoint; var currentClassName = methodInfo?.DeclaringType?.Name; var currentNamespaceName = methodInfo?.DeclaringType?.Namespace; var currentNamespace = GetNameSpaceDesciptorForPath(namespaces, currentNamespaceName, true); if (!currentNamespace.Classes.ContainsKey(currentClassName)) { currentNamespace.Classes.Add(currentClassName, new NativeClassDescription(currentClassName)); } var currentClass = currentNamespace.Classes[currentClassName]; if (!currentClass.Methods.ContainsKey(currentMethodName)) { currentClass.Methods.Add(currentMethodName, GetNativeMethodFrom(progammingPlaformNumber, methodInfo, nativeAttribute)); } } } return(namespaces); }