CompilerResults contains Type in VS, but not in Excel  
Author Message
Jan Meeusen





PostPosted: .NET Base Class Library, CompilerResults contains Type in VS, but not in Excel Top

Hello all

 

I'm writing come classes where code is generated and compiled at runtime (i cut the exception handling and so to keep only the relevant code)



Dim lsCode As String
Dim loCompiler As ICodeCompiler = Nothing
Dim loCodeProvider As New Microsoft.VisualBasic.VBCodeProvider
loCompiler = loCodeProvider.CreateCompiler()
Dim loParameters As CompilerParameters = New CompilerParameters
loParameters.GenerateInMemory = True
Environment.CurrentDirectory = IO.Path.GetDirectoryName(System.Reflection.Assembly.GetCallingAssembly().Location)
Dim lsaReferencedAssemblies() As String = New String() { _
"System.dll", _
"mscorlib.dll", _
"System.Management.dll", _
"Microsoft.VisualBasic.dll"}

If Not lsaReferencedAssemblies Is Nothing Then
For Each lsAssemblyDLL As String In lsaReferencedAssemblies
loParameters.ReferencedAssemblies.Add(lsAssemblyDLL)
Next lsAssemblyDLL
End If

AddHandler System.AppDomain.CurrentDomain.AssemblyResolve, AddressOf CurrentDomain_AssemblyResolve

Dim loRes As CompilerResults = loCompiler.CompileAssemblyFromSource(loParameters, lsCode )
RemoveHandler System.AppDomain.CurrentDomain.AssemblyResolve, AddressOf CurrentDomain_AssemblyResolve

If (Not loRes.Errors.HasErrors) Then
Dim loLoadedAssembly As System.Reflection.Assembly = loRes.CompiledAssembly
For Each m As System.Type In loRes.CompiledAssembly.GetTypes
MsgBox("loRes.CompiledAssembly.GetTypes: " & m.FullName)
Next
Dim mytype as Type = loLoadedAssembly.GetType("_ClassA")
Dim cl as IClassA = 
(CType(System.Activator.CreateInstance(mytype , New Object() {}), IClassA ))

End If
 

 

The code runs very smooth inside Visual Studio (i attached a form and ran some test from there). But when i compile it to a DLL and try to access it from there, i get an error on the CreateInstance. This is because GetType returns null/Nothing because it couldn't find the type.

 

Let me know if you could reproduce the issue and/or if you have any suggestions.

Thanks in advance,
Jhn

P.S.: is there a nice way to paste code here I pasted the above code, but had to remove all hard returns and change them to soft returns (whatever the name is: Enter => Shft-Enter).

 



.NET Development21  
 
 
CommonGenius.com





PostPosted: .NET Base Class Library, CompilerResults contains Type in VS, but not in Excel Top

Specify the full namespace along with the class name.

For posting code, use [code language="VB"]'Put your code here[/code]



 
 
John Musenee





PostPosted: .NET Base Class Library, CompilerResults contains Type in VS, but not in Excel Top

Thanks for replying.

I see now that -by accident- i put a Msgbox in my previous post:


For Each m As System.Type In loRes.CompiledAssembly.GetTypes
MsgBox("loRes.CompiledAssembly.GetTypes: " & m.FullName)
Next

If there would be a type, then this line should show it on the screen, right Excel, VS, or anywhere... So i suppose that if i'd put the full namespace there, it'll still return Nothing.
I'll try it tomorrow and let you know.

Regards,
Jhn


 
 
CommonGenius.com





PostPosted: .NET Base Class Library, CompilerResults contains Type in VS, but not in Excel Top

You indicated in your first post that your problem was that GetType was returning Nothing. Are you now saying that GetTypes is not returning any types at all



 
 
John Musenee





PostPosted: .NET Base Class Library, CompilerResults contains Type in VS, but not in Excel Top

You're right, i could've been more clear.

My original problem was that GetType resulted in Nothing; then i found out that GetTypes throws an error. But i didn't get much further...