示例#1
0
    static public Func <Object, Task <Object> > GetFunc(string assemblyFile, string typeName, string methodName)
    {
        var assembly = Assembly.LoadFrom(assemblyFile);
        var wrap     = ClrFuncReflectionWrap.Create(assembly, typeName, methodName);

        return(new Func <Object, Task <Object> >(wrap.Call));
    }
    public static ClrFuncReflectionWrap Create(Assembly assembly, String typeName, String methodName)
    {
        Type startupType = assembly.GetType(typeName, true, true);
        ClrFuncReflectionWrap wrap = new ClrFuncReflectionWrap();
        wrap.instance = System.Activator.CreateInstance(startupType, false);
        wrap.invokeMethod = startupType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public);
        if (wrap.invokeMethod == null)
        {
            throw new System.InvalidOperationException(
                "Unable to access the CLR method to wrap through reflection. Make sure it is a public instance method.");
        }

        return wrap;
    }
    public static ClrFuncReflectionWrap Create(Assembly assembly, String typeName, String methodName)
    {
        Type startupType           = assembly.GetType(typeName, true, true);
        ClrFuncReflectionWrap wrap = new ClrFuncReflectionWrap();

        wrap.instance     = System.Activator.CreateInstance(startupType, false);
        wrap.invokeMethod = startupType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public);
        if (wrap.invokeMethod == null)
        {
            throw new System.InvalidOperationException(
                      "Unable to access the CLR method to wrap through reflection. Make sure it is a public instance method.");
        }

        return(wrap);
    }
示例#4
0
    public static IntPtr GetFunc(string assemblyFile, string typeName, string methodName, IntPtr exception)
    {
        try
        {
            Marshal.WriteIntPtr(exception, IntPtr.Zero);

            Assembly assembly;

            if (assemblyFile.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || assemblyFile.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
            {
                if (!Path.IsPathRooted(assemblyFile))
                {
                    assemblyFile = Path.Combine(Directory.GetCurrentDirectory(), assemblyFile);
                }

                assembly = LoadContextAccessor.Default.LoadFile(assemblyFile);
            }

            else
            {
                assembly = LoadContextAccessor.Default.Load(new AssemblyName(assemblyFile));
            }

            DebugMessage("CoreCLREmbedding::GetFunc (CLR) - Assembly {0} loaded successfully", assemblyFile);

            ClrFuncReflectionWrap wrapper = ClrFuncReflectionWrap.Create(assembly, typeName, methodName);
            DebugMessage("CoreCLREmbedding::GetFunc (CLR) - Method {0}.{1}() loaded successfully", typeName, methodName);

            Func <object, Task <object> > wrapperFunc = wrapper.Call;
            GCHandle wrapperHandle = GCHandle.Alloc(wrapperFunc);

            return(GCHandle.ToIntPtr(wrapperHandle));
        }

        catch (Exception e)
        {
            DebugMessage("CoreCLREmbedding::GetFunc (CLR) - Exception was thrown: {0}", e.Message);

            V8Type v8Type;
            Marshal.WriteIntPtr(exception, MarshalCLRToV8(e, out v8Type));

            return(IntPtr.Zero);
        }
    }
    // create an instance of a reflection invokable wrapper of an CLR method
    public static ClrFuncReflectionWrap Create(Assembly assembly, string typeName, string methodName)
    {
        var startupType = assembly.GetType(typeName);

        if (startupType == null)
        {
            throw new TypeLoadException("Could not load type '" + typeName + "'");
        }

        var wrap = new ClrFuncReflectionWrap
        {
            instance     = Activator.CreateInstance(startupType),
            invokeMethod = startupType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public)
        };

        if (wrap.invokeMethod == null)
        {
            throw new System.InvalidOperationException(
                      "Unable to access the CLR method to wrap through reflection. Make sure it is a public instance method.");
        }

        return(wrap);
    }