Definition of common methods

That's all, I have a method that is currently used to call the DLL type of the return type bool, this works fine. This method

public static bool InvokeDLL(string strDllName, string strNameSpace, 
                             string strClassName, string strMethodName, 
                             ref object[] parameters, 
                             ref string strInformation, 
                             bool bWarnings = false)
{
    try
    {
        // Check if user has access to requested .dll.
        if (!File.Exists(Path.GetFullPath(strDllName)))
        {
            strInformation = String.Format("Cannot locate file '{0}'!",
                                           Path.GetFullPath(strDllName));
            return false;
        }
        else
        {
            // Execute the method from the requested .dll using reflection.
            Assembly DLL = Assembly.LoadFrom(Path.GetFullPath(strDllName));
            Type classType = DLL.GetType(String.Format("{0}.{1}", 
                                         strNameSpace, strClassName));
            if (classType != null)
            {
                object classInstance = Activator.CreateInstance(classType);
                MethodInfo methodInfo = classType.GetMethod(strMethodName);
                if (methodInfo != null)
                {
                    object result = null;
                    result = methodInfo.Invoke(classInstance, new object[] { parameters });
                    return Convert.ToBoolean(result);
                }
            }

            // Invocation failed fatally.
            strInformation = String.Format("Could not invoke the requested DLL '{0}'! " + 
                                           "Please insure that you have specified the namespace, class name " +
                                           "method and respective parameters correctly!",
                                           Path.GetFullPath(strDllName));
            return false;

        }
    }
    catch (Exception eX)
    {
        strInformation = String.Format("DLL Error: {0}!", eX.Message);
        if (bWarnings)
            Utils.ErrMsg(eX.Message);
        return false;
    }
}

Now I want to extend this method to get return values ​​from DLLs of any type. I planned to do this with the help of generics, but immediately went to unknown territory. How to return T when it is unknown at compile time, I looked at the thoughts, but I'm not sure how it will be used in this case. Make the first check in the code above.

public static T InvokeDLL<T>(string strDllName, string strNameSpace, 
                             string strClassName, string strMethodName, 
                             ref object[] parameters, ref string strInformation, 
                             bool bWarnings = false)
{
    try
    {
        // Check if user has access to requested .dll.
        if (!File.Exists(Path.GetFullPath(strDllName)))
        {
            strInformation = String.Format("Cannot locate file '{0}'!",
                                           Path.GetFullPath(strDllName));
            return "WHAT/HOW??";
        ...

How can I achieve what I want, or am I just overloading the method?

Many thanks for your help.

+5
source share
3 answers

Replace

return false;

by

return default(T);

and

return Convert.ToBoolean(result);

by

return (T)result;
+6
source

DLL, , , -. , T, - return default(T), (.. 0 int, null ).

, , .

+4

I would do something like this:

    public static InvokeDLLResult<T> InvokeDLL<T>(string strDllName, string strNameSpace,
                         string strClassName, string strMethodName,
                         ref object[] parameters,
                         ref string strInformation,
                         bool bWarnings = false)
    {
        try
        {
            // Check if user has access to requested .dll.
            if (!File.Exists(Path.GetFullPath(strDllName)))
            {
                strInformation = String.Format("Cannot locate file '{0}'!",
                                                         Path.GetFullPath(strDllName));
                return InvokeDLLResult<T>.Failure;
            }
            else
            {
                // Execute the method from the requested .dll using reflection.
                Assembly DLL = Assembly.LoadFrom(Path.GetFullPath(strDllName));
                Type classType = DLL.GetType(String.Format("{0}.{1}", strNameSpace, strClassName));
                if (classType != null)
                {
                    object classInstance = Activator.CreateInstance(classType);
                    MethodInfo methodInfo = classType.GetMethod(strMethodName);
                    if (methodInfo != null)
                    {
                        object result = null;
                        result = methodInfo.Invoke(classInstance, new object[] { parameters });
                        return new InvokeDLLResult<T>(true, (T) Convert.ChangeType(result, methodInfo.ReturnType));
                    }
                }

                // Invocation failed fatally.
                strInformation = String.Format("Could not invoke the requested DLL '{0}'! " +
                                                         "Please insure that you have specified the namespace, class name " +
                                                         "method and respective parameters correctly!",
                                                         Path.GetFullPath(strDllName));
                return InvokeDLLResult<T>.Failure;

            }
        }
        catch (Exception eX)
        {
            strInformation = String.Format("DLL Error: {0}!", eX.Message);
            if (bWarnings)
                Debug.WriteLine(eX.Message);
            return InvokeDLLResult<T>.Failure;
        }
    }

    public class InvokeDLLResult<T>
    {
        public InvokeDLLResult(bool success, T result)
        {
            Success = success;
            Result = result;
        }

        public bool Success { get; private set; }
        public T Result { get; private set; }

        public static InvokeDLLResult<T> Failure = new InvokeDLLResult<T>(false, default(T));
    }

Or, I would write a TryInvokeDLL method, using the out parameter to return the result.

+2
source

All Articles