HighTechTalks DotNet Forums  

Dynamic class creation at runtime

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss Dynamic class creation at runtime in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Piggy
 
Posts: n/a

Default Dynamic class creation at runtime - 07-10-2005 , 10:27 AM






Hi to all,

I’m developing a project in VB.NET 2003 to analyze the financial markets
using several algorithms. To do that I created a “mustinherit” class to
manage the behavior of the standard algorithm, and then a number of inherited
classes for each single algorithm.
Since the algorithms are potentially infinite, I will never be able to code
them all in the project at design time. So, I would like now to create an
editor to allow the users (using my software GUI) to dynamically create a new
class (inherited from the generic class) for each algorithm they want to use.
I do not have any idea on how I can do it. I read something about the
dynamic definition of Assemblies, but I do not want to create a new Assembly,
I would only add (and save) a new inherited class to my existing assembly.
Any help (samples, ideas, links) will be greatly appreciated.

This is the two classes I am using:

The generic class:

Public MustInherit Class IndexStrategy
Implements IDisposable



………


MustOverride Overloads Function GetTargetPortfolio (ByVal TargetDate as
Date) as MyType1
MustOverride Function GetParameters() as MyType2

End Class


Each inherited class represents a single algorithm:


Public Class Algorithm1
Inherits IndexStrategy

Public Sub New(IndexStrategy Parameters + specific parameters for the
algorithm1)
….
End Sub

Public Overrides Function GetTargetPortfolio (ByVal TargetDate as Date) as
MyType1
‘Here there is the code the user should be able to write to specify the
rules of the algorithm
End Function

Public Overrides Function GetParameters () as MyType2
‘I can manage to write this function automatically
End Function

End Class

Please, if this is not the correct group, please let me know the right one
(this is the very first time I am posting a question ).

Thanx very much

Reply With Quote
  #2  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: Dynamic class creation at runtime - 07-10-2005 , 11:23 AM






Piggy <Piggy (AT) discussions (DOT) microsoft.com> wrote:
Quote:
I?m developing a project in VB.NET 2003 to analyze the financial markets
using several algorithms. To do that I created a ?mustinherit? class to
manage the behavior of the standard algorithm, and then a number of inherited
classes for each single algorithm.
Since the algorithms are potentially infinite, I will never be able to code
them all in the project at design time. So, I would like now to create an
editor to allow the users (using my software GUI) to dynamically create a new
class (inherited from the generic class) for each algorithm they want to use.
I do not have any idea on how I can do it. I read something about the
dynamic definition of Assemblies, but I do not want to create a new Assembly,
I would only add (and save) a new inherited class to my existing assembly.
Any help (samples, ideas, links) will be greatly appreciated.
I'm not entirely sure which bit is causing you problems, but
Activator.CreateInstance is the way to go for creating new instances of
a type which is only known at runtime. You can find all the types in an
assembly which inherit from a given base class by using
Assembly.GetTypes and then checking IsAssignableFrom for each type,
passing in the base class. (This is better than using the BaseType
property, as that will only find classes that inherit directly from the
base class.)

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Reply With Quote
  #3  
Old   
Piggy
 
Posts: n/a

Default Re: Dynamic class creation at runtime - 07-11-2005 , 09:27 AM



Hi Jon,
thank you very much for your reply. My problem is not about creating an
instance of a type I am already doing this, now I would like to create
dinamically a new type that inherit directly from the base class. The new
type has to override a function of the base type, and the code of the
override is to be provided by the user at runtime. I also need to save the
new class.

Just for example:

I have in my assembly the base class and 2 inherited classes: BaseClass,
Class1 and Class2
At runtime I want to create Class3 (that inherits directly from the
BaseClass), just by typing the new code for the mustoverride function of the
BaseClass, and saving it. After doing that I need to be able to use (at
runtime) class3 in the same way I use Class1 and Class2.

Can you help me with this?

Thank you very much


"Jon Skeet [C# MVP]" wrote:

Quote:
Piggy <Piggy (AT) discussions (DOT) microsoft.com> wrote:
I?m developing a project in VB.NET 2003 to analyze the financial markets
using several algorithms. To do that I created a ?mustinherit? class to
manage the behavior of the standard algorithm, and then a number of inherited
classes for each single algorithm.
Since the algorithms are potentially infinite, I will never be able to code
them all in the project at design time. So, I would like now to create an
editor to allow the users (using my software GUI) to dynamically create a new
class (inherited from the generic class) for each algorithm they want to use.
I do not have any idea on how I can do it. I read something about the
dynamic definition of Assemblies, but I do not want to create a new Assembly,
I would only add (and save) a new inherited class to my existing assembly.
Any help (samples, ideas, links) will be greatly appreciated.

I'm not entirely sure which bit is causing you problems, but
Activator.CreateInstance is the way to go for creating new instances of
a type which is only known at runtime. You can find all the types in an
assembly which inherit from a given base class by using
Assembly.GetTypes and then checking IsAssignableFrom for each type,
passing in the base class. (This is better than using the BaseType
property, as that will only find classes that inherit directly from the
base class.)

--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Reply With Quote
  #4  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: Dynamic class creation at runtime - 07-11-2005 , 12:48 PM



Piggy <Piggy (AT) discussions (DOT) microsoft.com> wrote:
Quote:
thank you very much for your reply. My problem is not about creating an
instance of a type I am already doing this, now I would like to create
dinamically a new type that inherit directly from the base class. The new
type has to override a function of the base type, and the code of the
override is to be provided by the user at runtime. I also need to save the
new class.
Ah, I see. In that case, you should look at the System.Reflection.Emit
namespace and the System.CodeDom namespace. Both should have a fair
amount of documentation and examples in MSDN.

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Reply With Quote
  #5  
Old   
Piggy
 
Posts: n/a

Default Re: Dynamic class creation at runtime - 07-11-2005 , 02:15 PM



Thank you Jon.
I am looking at the System.Reflection.Emit and System.CodeDom namespaces

"Jon Skeet [C# MVP]" wrote:

Quote:
Piggy <Piggy (AT) discussions (DOT) microsoft.com> wrote:
thank you very much for your reply. My problem is not about creating an
instance of a type I am already doing this, now I would like to create
dinamically a new type that inherit directly from the base class. The new
type has to override a function of the base type, and the code of the
override is to be provided by the user at runtime. I also need to save the
new class.

Ah, I see. In that case, you should look at the System.Reflection.Emit
namespace and the System.CodeDom namespace. Both should have a fair
amount of documentation and examples in MSDN.

--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Reply With Quote
  #6  
Old   
john conwell
 
Posts: n/a

Default Re: Dynamic class creation at runtime - 07-13-2005 , 12:17 PM



Hey Piggy,
I'm in the middle of writing several articles on creating dynamic types at
runtime so I can help you with any specific questions you have. First, yes,
you'll have to use Reflection.Emit. the annoying thing about that is that
you have to emit IL opcodes into your type TypeBuilder.

You could build a string of C# or VB and then run it through the
CSharpCodeProvider or VBCodeProvider object to compile it, but this would be
a fairly big perf hit, not to mention that you'r app would have to load a new
assembly into memory after each compile.

With Reflection.Emit you can use the same assembly over and over, each time
you need to add a new dynamic class.

The general rules with dynamic types is to code against a public interface.
This is because you wont have any API to code against at design time. You
should probably wrap your type builder logic in a class factory design
pattern. This is useful since you wont be able to "new" a dynamic type.

Like I said before, if you have any specific questions let me know and I'd
be more than happy to help. Dynamic types are pretty cool to use.


"Piggy" wrote:

Quote:
Thank you Jon.
I am looking at the System.Reflection.Emit and System.CodeDom namespaces

"Jon Skeet [C# MVP]" wrote:

Piggy <Piggy (AT) discussions (DOT) microsoft.com> wrote:
thank you very much for your reply. My problem is not about creating an
instance of a type I am already doing this, now I would like to create
dinamically a new type that inherit directly from the base class. The new
type has to override a function of the base type, and the code of the
override is to be provided by the user at runtime. I also need to save the
new class.

Ah, I see. In that case, you should look at the System.Reflection.Emit
namespace and the System.CodeDom namespace. Both should have a fair
amount of documentation and examples in MSDN.

--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Reply With Quote
  #7  
Old   
Piggy
 
Posts: n/a

Default Re: Dynamic class creation at runtime - 07-13-2005 , 07:33 PM



Hi John,

I decided for the VBCodeProvider because I need to provide an editor
functionality to my project. Only the user (obviously at runtime) decides how
to override the critical function of the base class from which the new type
will inherit.
To do this I used the CodeSnippetUnit type to get the input text and
VBCodeProvider to:
1-Generate the code
2-Save the code
3-Compile a new dll with the newly created type

After creating the dll, I can load it throught Assembly.LoadFromFile.
Working with the type is now easy, since it inherits from the base class I
created in the main assembly.

As for the problem of loading an assembly for each new class I (hope) it
will not be a problem, only one of the new classes will be loaded of each
operation.

The following is the code I wrote. what do you think about it?

Public Sub SaveCode()
Dim LiteralCode As String = "Imports Microsoft.VisualBasic" & vbCrLf
& "Imports FinancialFormulas" & vbCrLf & CodeRTF.Text '<<-- In CodeRTF
(RichTextBox) the user writes the main code for the new class
Dim CSU As New CodeSnippetCompileUnit(LiteralCode)
Dim CodeWriter As IO.TextWriter =
IO.File.CreateText("d:\tests\Prova.vb")
Dim GOpts As New CodeGeneratorOptions
Dim VBCoder As New VBCodeProvider
Dim CGen As ICodeGenerator = VBCoder.CreateGenerator()

CSU.ReferencedAssemblies.Add("System")
CSU.ReferencedAssemblies.Add("Microsoft.VisualBasi c")

CSU.ReferencedAssemblies.Add("C:\VBProgies\NET\Opt ions\OptionFormulas\bin\FinancialFormulas.dll")

GOpts.ElseOnClosing = True
CGen.GenerateCodeFromCompileUnit(CSU, CodeWriter, New
System.CodeDom.Compiler.CodeGeneratorOptions)
CodeWriter.Close()
'CodeRTF.LoadFile("d:\tests\Prova.vb")

End Sub

Public Function AddClass() As Boolean
Dim VBCoder As New VBCodeProvider
Dim CComp As ICodeCompiler = VBCoder.CreateCompiler
Dim CParams As New CompilerParameters
Dim EEn As IEnumerator

If IO.File.Exists("d:\tests\Prova.dll") Then
IO.File.Delete("d:\tests\Prova.dll")

CParams.GenerateExecutable = False
CParams.OutputAssembly = "d:\tests\Prova.dll"

CParams.ReferencedAssemblies.Add("C:\VBProgies\NET \Options\OptionFormulas\bin\FinancialFormulas.dll" )

Dim Res As CompilerResults = CComp.CompileAssemblyFromFile(CParams,
"d:\tests\Prova.vb")

With Res
If .Errors.Count <> 0 Then
CodeRTF.Text &= vbCrLf & vbCrLf & "ERRORS FOUND!" & vbCrLf
EEn = .Errors.GetEnumerator
EEn.Reset()
While EEn.MoveNext
With CType(EEn.Current, CompilerError)
CodeRTF.Text &= .ErrorText & " (Row " & .Line & ")"
& vbCrLf
End With
End While
Return False
End If

End With

Return True

End Function

Public Function LoadClass()
Dim sperem As [Assembly] = [Assembly].LoadFile("d:\tests\Prova.dll")
Dim DynStrat As Type = sperem.GetType("DynInd")
Dim Params() As Type = {GetType(String), GetType(String),
GetType(Double), GetType(Double),
GetType(FinancialFormulas.EquityPortfolioStrategie s.SingleIndexStrategy.MembersNumberMethodEnum), GetType(Integer)}
Dim Values() As Object = {"Albert", "SX5E", CDbl(1000000000),
CDbl(15), CType(0,
FinancialFormulas.EquityPortfolioStrategies.Single IndexStrategy.MembersNumberMethodEnum), CType(13, Integer)}
Dim CInfo As ConstructorInfo = DynStrat.GetConstructor(Params)
Dim Strat As Object

Try
Strat = CInfo.Invoke(Values)
Catch ex As Exception

End Try
MsgBox(Strat.GetTargetPortfolio(Now.Date))

End Function


Thank you very much


"john conwell" wrote:

Quote:
Hey Piggy,
I'm in the middle of writing several articles on creating dynamic types at
runtime so I can help you with any specific questions you have. First, yes,
you'll have to use Reflection.Emit. the annoying thing about that is that
you have to emit IL opcodes into your type TypeBuilder.

You could build a string of C# or VB and then run it through the
CSharpCodeProvider or VBCodeProvider object to compile it, but this would be
a fairly big perf hit, not to mention that you'r app would have to load a new
assembly into memory after each compile.

With Reflection.Emit you can use the same assembly over and over, each time
you need to add a new dynamic class.

The general rules with dynamic types is to code against a public interface.
This is because you wont have any API to code against at design time. You
should probably wrap your type builder logic in a class factory design
pattern. This is useful since you wont be able to "new" a dynamic type.

Like I said before, if you have any specific questions let me know and I'd
be more than happy to help. Dynamic types are pretty cool to use.


"Piggy" wrote:

Thank you Jon.
I am looking at the System.Reflection.Emit and System.CodeDom namespaces

"Jon Skeet [C# MVP]" wrote:

Piggy <Piggy (AT) discussions (DOT) microsoft.com> wrote:
thank you very much for your reply. My problem is not about creating an
instance of a type I am already doing this, now I would like to create
dinamically a new type that inherit directly from the base class. The new
type has to override a function of the base type, and the code of the
override is to be provided by the user at runtime. I also need to save the
new class.

Ah, I see. In that case, you should look at the System.Reflection.Emit
namespace and the System.CodeDom namespace. Both should have a fair
amount of documentation and examples in MSDN.

--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Reply With Quote
  #8  
Old   
Rahul Anand
 
Posts: n/a

Default Re: Dynamic class creation at runtime - 07-22-2005 , 07:00 AM



Hi Piggy,

Just to correct your code example the first statement of LoadClass should
use LoadFrom instead of LoadFile

-------------------------------------
Public Function LoadClass()
Dim sperem As [Assembly] = [Assembly].LoadFrom("d:\tests\Prova.dll")
-------------------------------------

Hope it will save some debugging time of the readers who wants to try this
code.

--
Cheers,
Rahul Anand

"Piggy" wrote:

Quote:
Hi John,

I decided for the VBCodeProvider because I need to provide an editor
functionality to my project. Only the user (obviously at runtime) decides how
to override the critical function of the base class from which the new type
will inherit.
To do this I used the CodeSnippetUnit type to get the input text and
VBCodeProvider to:
1-Generate the code
2-Save the code
3-Compile a new dll with the newly created type

After creating the dll, I can load it throught Assembly.LoadFromFile.
Working with the type is now easy, since it inherits from the base class I
created in the main assembly.

As for the problem of loading an assembly for each new class I (hope) it
will not be a problem, only one of the new classes will be loaded of each
operation.

The following is the code I wrote. what do you think about it?

Public Sub SaveCode()
Dim LiteralCode As String = "Imports Microsoft.VisualBasic" & vbCrLf
& "Imports FinancialFormulas" & vbCrLf & CodeRTF.Text '<<-- In CodeRTF
(RichTextBox) the user writes the main code for the new class
Dim CSU As New CodeSnippetCompileUnit(LiteralCode)
Dim CodeWriter As IO.TextWriter =
IO.File.CreateText("d:\tests\Prova.vb")
Dim GOpts As New CodeGeneratorOptions
Dim VBCoder As New VBCodeProvider
Dim CGen As ICodeGenerator = VBCoder.CreateGenerator()

CSU.ReferencedAssemblies.Add("System")
CSU.ReferencedAssemblies.Add("Microsoft.VisualBasi c")

CSU.ReferencedAssemblies.Add("C:\VBProgies\NET\Opt ions\OptionFormulas\bin\FinancialFormulas.dll")

GOpts.ElseOnClosing = True
CGen.GenerateCodeFromCompileUnit(CSU, CodeWriter, New
System.CodeDom.Compiler.CodeGeneratorOptions)
CodeWriter.Close()
'CodeRTF.LoadFile("d:\tests\Prova.vb")

End Sub

Public Function AddClass() As Boolean
Dim VBCoder As New VBCodeProvider
Dim CComp As ICodeCompiler = VBCoder.CreateCompiler
Dim CParams As New CompilerParameters
Dim EEn As IEnumerator

If IO.File.Exists("d:\tests\Prova.dll") Then
IO.File.Delete("d:\tests\Prova.dll")

CParams.GenerateExecutable = False
CParams.OutputAssembly = "d:\tests\Prova.dll"

CParams.ReferencedAssemblies.Add("C:\VBProgies\NET \Options\OptionFormulas\bin\FinancialFormulas.dll" )

Dim Res As CompilerResults = CComp.CompileAssemblyFromFile(CParams,
"d:\tests\Prova.vb")

With Res
If .Errors.Count <> 0 Then
CodeRTF.Text &= vbCrLf & vbCrLf & "ERRORS FOUND!" & vbCrLf
EEn = .Errors.GetEnumerator
EEn.Reset()
While EEn.MoveNext
With CType(EEn.Current, CompilerError)
CodeRTF.Text &= .ErrorText & " (Row " & .Line & ")"
& vbCrLf
End With
End While
Return False
End If

End With

Return True

End Function

Public Function LoadClass()
Dim sperem As [Assembly] = [Assembly].LoadFile("d:\tests\Prova.dll")
Dim DynStrat As Type = sperem.GetType("DynInd")
Dim Params() As Type = {GetType(String), GetType(String),
GetType(Double), GetType(Double),
GetType(FinancialFormulas.EquityPortfolioStrategie s.SingleIndexStrategy.MembersNumberMethodEnum), GetType(Integer)}
Dim Values() As Object = {"Albert", "SX5E", CDbl(1000000000),
CDbl(15), CType(0,
FinancialFormulas.EquityPortfolioStrategies.Single IndexStrategy.MembersNumberMethodEnum), CType(13, Integer)}
Dim CInfo As ConstructorInfo = DynStrat.GetConstructor(Params)
Dim Strat As Object

Try
Strat = CInfo.Invoke(Values)
Catch ex As Exception

End Try
MsgBox(Strat.GetTargetPortfolio(Now.Date))

End Function


Thank you very much


"john conwell" wrote:

Hey Piggy,
I'm in the middle of writing several articles on creating dynamic types at
runtime so I can help you with any specific questions you have. First, yes,
you'll have to use Reflection.Emit. the annoying thing about that is that
you have to emit IL opcodes into your type TypeBuilder.

You could build a string of C# or VB and then run it through the
CSharpCodeProvider or VBCodeProvider object to compile it, but this would be
a fairly big perf hit, not to mention that you'r app would have to load a new
assembly into memory after each compile.

With Reflection.Emit you can use the same assembly over and over, each time
you need to add a new dynamic class.

The general rules with dynamic types is to code against a public interface.
This is because you wont have any API to code against at design time. You
should probably wrap your type builder logic in a class factory design
pattern. This is useful since you wont be able to "new" a dynamic type.

Like I said before, if you have any specific questions let me know and I'd
be more than happy to help. Dynamic types are pretty cool to use.


"Piggy" wrote:

Thank you Jon.
I am looking at the System.Reflection.Emit and System.CodeDom namespaces

"Jon Skeet [C# MVP]" wrote:

Piggy <Piggy (AT) discussions (DOT) microsoft.com> wrote:
thank you very much for your reply. My problem is not about creating an
instance of a type I am already doing this, now I would like to create
dinamically a new type that inherit directly from the base class. The new
type has to override a function of the base type, and the code of the
override is to be provided by the user at runtime. I also need to save the
new class.

Ah, I see. In that case, you should look at the System.Reflection.Emit
namespace and the System.CodeDom namespace. Both should have a fair
amount of documentation and examples in MSDN.

--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Reply With Quote
  #9  
Old   
Piggy
 
Posts: n/a

Default Re: Dynamic class creation at runtime - 07-23-2005 , 11:38 AM



Hi Rahul,

It is correct to use LoadFrom instead of LoadFile. Unfortunately, I changed
the statement in my code, but forgot to post the correction.... Sorry



"Rahul Anand" wrote:

Quote:
Hi Piggy,

Just to correct your code example the first statement of LoadClass should
use LoadFrom instead of LoadFile

-------------------------------------
Public Function LoadClass()
Dim sperem As [Assembly] = [Assembly].LoadFrom("d:\tests\Prova.dll")
-------------------------------------

Hope it will save some debugging time of the readers who wants to try this
code.

--
Cheers,
Rahul Anand

"Piggy" wrote:

Hi John,

I decided for the VBCodeProvider because I need to provide an editor
functionality to my project. Only the user (obviously at runtime) decides how
to override the critical function of the base class from which the new type
will inherit.
To do this I used the CodeSnippetUnit type to get the input text and
VBCodeProvider to:
1-Generate the code
2-Save the code
3-Compile a new dll with the newly created type

After creating the dll, I can load it throught Assembly.LoadFromFile.
Working with the type is now easy, since it inherits from the base class I
created in the main assembly.

As for the problem of loading an assembly for each new class I (hope) it
will not be a problem, only one of the new classes will be loaded of each
operation.

The following is the code I wrote. what do you think about it?

Public Sub SaveCode()
Dim LiteralCode As String = "Imports Microsoft.VisualBasic" & vbCrLf
& "Imports FinancialFormulas" & vbCrLf & CodeRTF.Text '<<-- In CodeRTF
(RichTextBox) the user writes the main code for the new class
Dim CSU As New CodeSnippetCompileUnit(LiteralCode)
Dim CodeWriter As IO.TextWriter =
IO.File.CreateText("d:\tests\Prova.vb")
Dim GOpts As New CodeGeneratorOptions
Dim VBCoder As New VBCodeProvider
Dim CGen As ICodeGenerator = VBCoder.CreateGenerator()

CSU.ReferencedAssemblies.Add("System")
CSU.ReferencedAssemblies.Add("Microsoft.VisualBasi c")

CSU.ReferencedAssemblies.Add("C:\VBProgies\NET\Opt ions\OptionFormulas\bin\FinancialFormulas.dll")

GOpts.ElseOnClosing = True
CGen.GenerateCodeFromCompileUnit(CSU, CodeWriter, New
System.CodeDom.Compiler.CodeGeneratorOptions)
CodeWriter.Close()
'CodeRTF.LoadFile("d:\tests\Prova.vb")

End Sub

Public Function AddClass() As Boolean
Dim VBCoder As New VBCodeProvider
Dim CComp As ICodeCompiler = VBCoder.CreateCompiler
Dim CParams As New CompilerParameters
Dim EEn As IEnumerator

If IO.File.Exists("d:\tests\Prova.dll") Then
IO.File.Delete("d:\tests\Prova.dll")

CParams.GenerateExecutable = False
CParams.OutputAssembly = "d:\tests\Prova.dll"

CParams.ReferencedAssemblies.Add("C:\VBProgies\NET \Options\OptionFormulas\bin\FinancialFormulas.dll" )

Dim Res As CompilerResults = CComp.CompileAssemblyFromFile(CParams,
"d:\tests\Prova.vb")

With Res
If .Errors.Count <> 0 Then
CodeRTF.Text &= vbCrLf & vbCrLf & "ERRORS FOUND!" & vbCrLf
EEn = .Errors.GetEnumerator
EEn.Reset()
While EEn.MoveNext
With CType(EEn.Current, CompilerError)
CodeRTF.Text &= .ErrorText & " (Row " & .Line & ")"
& vbCrLf
End With
End While
Return False
End If

End With

Return True

End Function

Public Function LoadClass()
Dim sperem As [Assembly] = [Assembly].LoadFile("d:\tests\Prova.dll")
Dim DynStrat As Type = sperem.GetType("DynInd")
Dim Params() As Type = {GetType(String), GetType(String),
GetType(Double), GetType(Double),
GetType(FinancialFormulas.EquityPortfolioStrategie s.SingleIndexStrategy.MembersNumberMethodEnum), GetType(Integer)}
Dim Values() As Object = {"Albert", "SX5E", CDbl(1000000000),
CDbl(15), CType(0,
FinancialFormulas.EquityPortfolioStrategies.Single IndexStrategy.MembersNumberMethodEnum), CType(13, Integer)}
Dim CInfo As ConstructorInfo = DynStrat.GetConstructor(Params)
Dim Strat As Object

Try
Strat = CInfo.Invoke(Values)
Catch ex As Exception

End Try
MsgBox(Strat.GetTargetPortfolio(Now.Date))

End Function


Thank you very much


"john conwell" wrote:

Hey Piggy,
I'm in the middle of writing several articles on creating dynamic types at
runtime so I can help you with any specific questions you have. First, yes,
you'll have to use Reflection.Emit. the annoying thing about that is that
you have to emit IL opcodes into your type TypeBuilder.

You could build a string of C# or VB and then run it through the
CSharpCodeProvider or VBCodeProvider object to compile it, but this would be
a fairly big perf hit, not to mention that you'r app would have to load a new
assembly into memory after each compile.

With Reflection.Emit you can use the same assembly over and over, each time
you need to add a new dynamic class.

The general rules with dynamic types is to code against a public interface.
This is because you wont have any API to code against at design time. You
should probably wrap your type builder logic in a class factory design
pattern. This is useful since you wont be able to "new" a dynamic type.

Like I said before, if you have any specific questions let me know and I'd
be more than happy to help. Dynamic types are pretty cool to use.


"Piggy" wrote:

Thank you Jon.
I am looking at the System.Reflection.Emit and System.CodeDom namespaces

"Jon Skeet [C# MVP]" wrote:

Piggy <Piggy (AT) discussions (DOT) microsoft.com> wrote:
thank you very much for your reply. My problem is not about creating an
instance of a type I am already doing this, now I would like to create
dinamically a new type that inherit directly from the base class. The new
type has to override a function of the base type, and the code of the
override is to be provided by the user at runtime. I also need to save the
new class.

Ah, I see. In that case, you should look at the System.Reflection.Emit
namespace and the System.CodeDom namespace. Both should have a fair
amount of documentation and examples in MSDN.

--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.