Jelajahi Sumber

adding support for nested .net types

Fred VanEijk 10 tahun lalu
induk
melakukan
17f8d283db

+ 9 - 1
Jint.Tests/Runtime/Domain/Shape.cs

@@ -15,7 +15,15 @@ namespace Shapes
     }
 
     public class Circle : Shape
-    {
+    {
+
+        public enum Kind
+        {
+          Unit,
+          Ellipse,
+          Round = 5
+        }
+
         public Circle()
         {
         }

+ 12 - 0
Jint.Tests/Runtime/InteropTests.cs

@@ -1143,5 +1143,17 @@ namespace Jint.Tests.Runtime
             ");
         }
 
+        [Fact]
+        public void ShouldImportNamespaceNestedType()
+        {
+          RunTest(@"
+                var shapes = importNamespace('Shapes.Circle');
+                var kinds = shapes.Kind;
+                assert(kinds.Unit === 0);
+                assert(kinds.Ellipse === 1);
+                assert(kinds.Round === 5);
+            ");
+        }
+
     }
 }

+ 14 - 0
Jint/Runtime/Interop/NamespaceReference.cs

@@ -119,6 +119,20 @@ namespace Jint.Runtime.Interop
                     Engine.TypeCache.Add(path, type);
                     return TypeReference.CreateTypeReference(Engine, type);
                 }
+				       
+                var lastPeriodPos = path.LastIndexOf(".");
+				        var trimPath = path.Substring(0, lastPeriodPos);
+				        var typeName = path.Substring(lastPeriodPos+1, path.Length - lastPeriodPos-1);
+				        type = assembly.GetType(trimPath);
+				        if (type != null)
+				        foreach (Type nType in type.GetNestedTypes(BindingFlags.Public))
+				        {
+					        if (nType.FullName.Contains(trimPath+"+"+typeName))
+					        {
+						        Engine.TypeCache.Add(path, nType);
+						        return TypeReference.CreateTypeReference(Engine, nType);
+					        }
+				        }
             }
 
             // the new path doesn't represent a known class, thus return a new namespace instance