Debugging code from dynamically loaded assembly in .net core 2.0

前端 未结 5 654
一向
一向 2021-02-05 07:27

I have a .net core 2.0 console app that does the following (simplified):

var a = Assembly.Load(Assembly.GetEntryAssembly()
                              .GetRefe         


        
5条回答
  •  你的背包
    2021-02-05 07:34

    You code will not compile if you use Assembly.LoadFile way.

    I'have created a sample project in Visual studio 2017 community and I was able to step into a function from dynamic assembly by using both Assembly.LoadFile and Assembly.Loadways. I did not go with you code since it was not compiling, but i will provide a solution which i think you will solve your problem.

    Here is the solution: Full working solution is here https://github.com/beyazc/.netcore-dynamic-assembly-debug

    Basicly I was able yo debug with following code. You should press f11 on invoke method

    var assemlies = Assembly.GetEntryAssembly().GetReferencedAssemblies();
            var assemblyName = "";
            foreach (var item in assemlies)
            {
                if (item.Name == "RefProj")
                    assemblyName = item.Name;
    
            }
            var path = new FileInfo(Assembly.GetExecutingAssembly().Location);
            var a = Assembly.LoadFile(Path.Combine($@"{path}\..\..\..\..\..\RefProj\bin\Debug\netstandard2.0", "RefProj.dll"));
            var t = a.GetType("RefProj.Class1");
    
            var i = Activator.CreateInstance(t);
            MethodInfo mi = t.GetMethod("get1");
            mi.Invoke(i, null);
    

提交回复
热议问题