none
如何通过assebmly反射获取list<t>返回值 RRS feed

答案

  • 有没有尝试过什么代码?这段代码可以供你参考:

    SampleLibrary.dll

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace SampleLibrary
    {
        public class SampleClass
        {
            public List<string> SampleMethod()
            {
                return new List<string>() { 
                    "hello",
                    "world"
                };
            }
        }
    }
    

    主程序:

    Assembly assembly = Assembly.LoadFrom("SampleLibrary.dll");
                Type sampleClassType = assembly.GetType("SampleLibrary.SampleClass");
                object sampleClass=Activator.CreateInstance(sampleClassType);
    
                MethodInfo sampleMethodInfo=sampleClassType.GetMethod("SampleMethod");
    
                object returnValue=sampleMethodInfo.Invoke(sampleClass, null);
    
                Console.WriteLine(returnValue.GetType().ToString());
    
                List<string> list = returnValue as List<string>;
    
                if (list != null)
                {
                    foreach (string s in list)
                    {
                        Console.WriteLine(s);
                    }
                }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年3月25日 3:34
    版主

全部回复

  • 你好:

    可以详细描述一下你的问题吗?


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年3月23日 8:19
    版主
  • 有一个DLL文件 里面有个封装了一个方法返回值是List<string>  我想用反射 获取返回值 
    2015年3月24日 15:17
  • 有没有尝试过什么代码?这段代码可以供你参考:

    SampleLibrary.dll

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace SampleLibrary
    {
        public class SampleClass
        {
            public List<string> SampleMethod()
            {
                return new List<string>() { 
                    "hello",
                    "world"
                };
            }
        }
    }
    

    主程序:

    Assembly assembly = Assembly.LoadFrom("SampleLibrary.dll");
                Type sampleClassType = assembly.GetType("SampleLibrary.SampleClass");
                object sampleClass=Activator.CreateInstance(sampleClassType);
    
                MethodInfo sampleMethodInfo=sampleClassType.GetMethod("SampleMethod");
    
                object returnValue=sampleMethodInfo.Invoke(sampleClass, null);
    
                Console.WriteLine(returnValue.GetType().ToString());
    
                List<string> list = returnValue as List<string>;
    
                if (list != null)
                {
                    foreach (string s in list)
                    {
                        Console.WriteLine(s);
                    }
                }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年3月25日 3:34
    版主