Browse Source

Adding importNamespace

Sebastien Ros 11 years ago
parent
commit
f75857b3b8
3 changed files with 11 additions and 3 deletions
  1. 4 0
      Jint/Engine.cs
  2. 2 1
      Jint/Runtime/Interop/TypeReference.cs
  3. 5 2
      README.md

+ 4 - 0
Jint/Engine.cs

@@ -120,6 +120,10 @@ namespace Jint
             if (Options.IsClrAllowed())
             {
                 Global.FastAddProperty("System", new NamespaceReference(this, "System"), false, false, false);
+                Global.FastAddProperty("importNamespace", new ClrFunctionInstance(this, (thisObj, arguments) =>
+                {
+                    return new NamespaceReference(this, TypeConverter.ToString(arguments.At(0)));
+                }), false, false, false);
             }
         }
 

+ 2 - 1
Jint/Runtime/Interop/TypeReference.cs

@@ -61,7 +61,8 @@ namespace Jint.Runtime.Interop
                             CultureInfo.InvariantCulture);
                     }
 
-                    var result =  TypeConverter.ToObject(Engine, JsValue.FromObject(Engine, method.Invoke(Type, parameters.ToArray())));
+                    var constructor = (ConstructorInfo)method;
+                    var result = TypeConverter.ToObject(Engine, JsValue.FromObject(Engine, constructor.Invoke(parameters.ToArray())));
 
                     // todo: cache method info
 

+ 5 - 2
README.md

@@ -77,9 +77,12 @@ And even create shortcuts to commong .NET methods
     jint> log('Hello World !');
     => "Hello World !"
 
-Loading custom assemblies dynamically can be done by using standard reflection
+Loading custom assemblies dynamically can be done by using standard reflection `System.Reflection.Assembly.Load`. You will also need
+to assign local namespace the same way `System` does it for you, by using `importNamespace`:
 
-    var import = System.Reflection.Assembly.Load;
+    var Foo = importNamespace('Foo');
+    var bar = new Foo.Bar();
+    log(bar.ToString());
     
 ## Implemented features: