none
Reflection, C++, C#, and Vb.Net RRS feed

  • Question

  • I am working on a project that serves as a robot tester, by crawling through a bunch of data files and running an application against those data files.  This tester application is written in VB.Net, using .Net Framework 4.0.  The application being tested is a VB.Net desktop application, but uses supporting components written in C++/CLI, C#, and VB.Net.  All of the components of this project use .Net 4.0.  The main VB.Net application uses Framework 4.5. The C++ component uses dependency injection to load a class that is defined in C#.  The C# class implements an interface that is defined in the C++ component.  Here is the C++ interface definition, inside of the CLICore namespace:

    namespace CLICore
    {
    	public interface class IConnGen {
    		List<TCC::Conns::MomConn^>^ GenerateConns(TMainDocument^ document);
    	};
    
    	public ref class ConnGen abstract sealed
    	{
    	private:
    		static Func<TCC::Conns::IConnDesignProfile^, IConnGen^>^ _Factory;
    	public:
    		static property Func<TCC::Conns::IConnDesignProfile^, IConnGen^>^ Factory
    		{
    			Func<TCC::Conns::IConnDesignProfile^, IConnGen^>^ get()
    			{
    				return _Factory;
    			}
    			void set(Func<TCC::Conns::IConnDesignProfile^, IConnGen^>^ value)
    			{
    				_Factory = value;
    			}
    		}
    	};
    
    }

    Here is a C# implementation of IConnGen, in the TCC.Conns namespace

    namespace TCC.Conns

    {

    public abstract class ConnGenBase : CLICore.IConnGen { protected IConnDesignProfile DesignProfile { get; set; } protected ConnGenBase(IConnDesignProfile profile) { DesignProfile = profile; } public List<MomConn> GenerateConns(TMainDocument document) { List<MomConn> connList = new List<MomConn>(); connList.AddRange(GenRConns(document)); return connList; } }

    }

    Here is the C# definition of the IConnDesignProfile interface, in the TCC.Conns namespace:


        

    public interface IConnDesignProfile { DesignProfileKey Key { get; set; } }


    Here is the C# class definition for ConnDesignProfile:

    namespace TCC.Conns
    {
     
        public static class ConnDesignProfile
        {
            public static Func<IConnDesignProfile> Factory { get; set; }
        }
    
    }


    Here is the actual instantiation of an object that implements the IConnGen interface:

    void DesignConns(TMainDocument^ mainDoc)
    {

    TCC::Conns::IConnDesignProfile^ designprofile = TCC::Conns::ConnDesignProfile::Factory->Invoke(); CLICore::IConnGen^ conngensvc = CLICore::ConnGen::Factory->Invoke(designprofile);

    }


    Here is the problem:  All of this works fine, except when I load the DLL that creates TMainDocument through Reflection.  Then, I get the following error when I try to execute the second of the two lines above: 

    "Could not load type 'CLICore.IConnGen' from assembly 'Core, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'."

    Why can I instantiate an object that implements IConnGen when I load the TMainDocument via a direct reference, but not when I load it with Reflection?


    	


    Wednesday, November 18, 2015 9:07 PM

Answers

  • Hi CountryDeveloper,

    For that kind of problem, to find the root cause. Debugging with the repro is the best way.

    Per my understanding, the cause could be any number of things. Likely causes are:

    1. The assembly cannot be found
    2. An assembly that your assembly depends upon cannot be found
    3. The assembly is found but the type isn't in it
    4. The type's static constructor throws an exception

    Please try to use Fuslogvw.exe (Assembly Binding Log Viewer) to diagnose why the .NET Framework cannot locate an assembly at run time.

    https://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.110).aspx

    Hope this helps.

    Best regards,

    Kristin


    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.

    Thursday, November 19, 2015 6:19 AM