@@ -47,24 +47,23 @@ public MathFuncAssemblyCecil(string namespaceName = "MathFuncLib", string classN
4747 ClassName = className ;
4848 }
4949
50- public void CompileFuncAndDerivative ( string expression , string variable , string fileName = "" )
50+ public void CompileFuncAndDerivativeToFile ( string expression , string variable , string fileName = "" )
5151 {
52- CompileFuncAndDerivative ( expression , variable , Path . GetDirectoryName ( fileName ) , Path . GetFileName ( fileName ) ) ;
52+ CompileFuncAndDerivativeToFile ( expression , variable , Path . GetDirectoryName ( fileName ) , Path . GetFileName ( fileName ) ) ;
5353 }
5454
55- public void CompileFuncAndDerivative ( string expression , string variable , string filePath = "" , string name = "" )
55+ public void CompileFuncAndDerivativeToFile ( string expression , string variable , string filePath = "" , string name = "" )
5656 {
57- var func = new MathFunc ( expression , variable , true , true ) ;
58- var funcDer = new MathFunc ( expression , variable , true , false ) . GetDerivative ( ) . GetPrecompilied ( ) ;
59-
60- Init ( name ) ;
61-
62- func . Compile ( this , FuncName ) ;
63- funcDer . Compile ( this , FuncDerivativeName ) ;
64-
65- Finalize ( filePath , name ) ;
57+ CompileFuncAndDerivative ( expression , variable , name ) ;
58+ SaveToFile ( filePath , name ) ;
6659 }
6760
61+ public byte [ ] CompileFuncAndDerivativeInMemory ( string expression , string variable , string name = "" )
62+ {
63+ CompileFuncAndDerivative ( expression , variable , name ) ;
64+ return SaveToBytes ( ) ;
65+ }
66+
6867 public void Init ( string fileName = "MathFuncLib.dll" )
6968 {
7069 var name = new AssemblyNameDefinition ( Path . GetFileNameWithoutExtension ( fileName ) , new Version ( 1 , 0 , 0 , 0 ) ) ;
@@ -86,12 +85,35 @@ public void Init(string fileName = "MathFuncLib.dll")
8685 Class . Methods . Add ( method ) ;
8786 }
8887
89- public void Finalize ( string path = "" , string fileName = "" )
88+ public void SaveToFile ( string path = "" , string fileName = "" )
9089 {
9190 Assembly . MainModule . Types . Add ( Class ) ;
9291 Assembly . Write ( Path . Combine ( path , string . IsNullOrEmpty ( fileName ) ? NamespaceName + ".dll" : fileName ) ) ;
9392 }
9493
94+ public byte [ ] SaveToBytes ( )
95+ {
96+ Assembly . MainModule . Types . Add ( Class ) ;
97+ byte [ ] result = null ;
98+ using ( var stream = new MemoryStream ( ) )
99+ {
100+ Assembly . Write ( stream ) ;
101+ result = stream . ToArray ( ) ;
102+ }
103+ return result ;
104+ }
105+
106+ private void CompileFuncAndDerivative ( string expression , string variable , string name = "" )
107+ {
108+ var func = new MathFunc ( expression , variable , true , true ) ;
109+ var funcDer = new MathFunc ( expression , variable , true , false ) . GetDerivative ( ) . GetPrecompilied ( ) ;
110+
111+ Init ( name ) ;
112+
113+ func . Compile ( this , FuncName ) ;
114+ funcDer . Compile ( this , FuncDerivativeName ) ;
115+ }
116+
95117 private void ImportMath ( AssemblyDefinition assembly )
96118 {
97119 TypesReferences = new Dictionary < KnownFuncType , MethodReference > ( ) ;
0 commit comments