Exemplo n.º 1
0
 private void CreateExternMethods(NamespaceDefinition globalNamespace, ClassDefinition methodsClass)
 {
     foreach (var @class in EnumerateClasses(globalNamespace).OrderBy(c => c.Name))
     {
         foreach (var method in EnumerateMethods(@class))
         {
             string name = GetExternMethodName(method);
             ParameterDefinition[] parameters = GetExternMethodParameters(method);
             var nativeMethod = new MethodDefinition(name, parameters)
             {
                 IsExtern = true,
                 IsStatic = true
             };
             if (method.IsConstructor)
             {
                 nativeMethod.ReturnType = TypeRefDefinition.IntPtr;
             }
             methodsClass.AddChild(nativeMethod);
         }
     }
 }
        public void CreatesNativeMethodImports()
        {
            var @namespace = new NamespaceDefinition();
            var @class     = new ClassDefinition("CppClass");
            var method     = new MethodDefinition("method");

            @namespace.AddChild(@class);
            @class.AddChild(method);

            var rootFolder = new RootFolderDefinition("root");
            var header     = new HeaderDefinition("header.h");

            rootFolder.AddChild(header);

            header.AddNode(@class);

            new DotNetTransformer().Transform(@namespace, rootFolder);

            var methodsHeader = rootFolder.Children.FirstOrDefault(c => c.Name == "UnsafeNativeMethods.cs");

            Assert.IsNotNull(methodsHeader);

            var methodsClass = @namespace.Children.FirstOrDefault(c => c.Name == "UnsafeNativeMethods") as ClassDefinition;

            Assert.IsNotNull(methodsClass);

            Assert.That(methodsClass.Children, Has.Count.EqualTo(1));
            var nativeMethod = methodsClass.Methods.First();

            Assert.AreEqual("CppClass_method", nativeMethod.Name);
            Assert.IsTrue(nativeMethod.IsStatic);
            Assert.IsTrue(nativeMethod.IsExtern);

            Assert.That(nativeMethod.Parameters, Has.Length.EqualTo(1));
            var objParameter = nativeMethod.Parameters[0];

            Assert.AreEqual("obj", objParameter.Name);
        }