????????
??????????????????????????????????? C# ?????????????????з??????
?????????????????????????????????? C# ?????????????????????????????????÷????á???????????????????????????????Щ????????????????????????????????????
???????????????????????? Seven Framework ??????????????????????????????????https://github.com/53V3N1X/SevenFramework??
???????????
??????????????????????? C# ?д???????????????????? System.Object ?????????????????????????????????????????int??double??decimal???????????????????????????????EXAMPLE 1 ????????????????????δ??????? C# ????????????
????// EXAMPLE 1 ----------------------------------------
????namespace ConsoleApplication
????{   public class Program  
????{  public static void Main(string[] args)    
????{  Sum<int>(new int[] { 1?? 2?? 3?? 4?? 5 });     }    
????public static T Sum<T>(T[] array)    
????{  T sum = 0; // (1) cannot convert int to generic      
????for (int i = 0; i < array.Length; i++)        
????sum += array[i]; // (2) cannot assume addition operator on generic      
????return sum;     }  
????}
????}
??????????EXAMPLE 1 ????????????????
????????”T”???????int?????????????
????????”T”???????????????????
?????????????????????????????????????Щ???????????
????????????????
????C#?е? where ????????????????????????????????????????????????????????????C#?е????????????C#????????????????????????????????????????????????????????????????????EXAMPLE 2 ????????????????????????????????????????????????????????????
????Hide Copy Code
????// EXAMPLE 2 ----------------------------------------
????namespace ConsoleApplication {  
????public class Program  
????{    
????public static void Main(string[] args)   
????{      
????Sum<int>(new int[] { 1?? 2?? 3?? 4?? 5 });    
????}    
????public static T Sum<T>(T[] array)      
????where T : number  // (1) there is no base "number" type in C#    
????{       T sum = 0;      
????for (int i = 0; i < array.Length; i++)        
????sum += array[i];      
????return sum;     }  
????}
????}
???????? EXAMPLE 2 ??????????????
??????C#????л???“???”?????
????????????????????????????“???”????????????????????????????????????????????????????C#???????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????? C?? ?е???????????????????????????????? int??double??decimal ??????????????????????????????????????д??????????????????C#?е????????д???????
????????3?У???????????????????????“????”??????????int????????Integer32??
????// EXAMPLE 3 ----------------------------------------
????namespace ConsoleApplication
????{  
????public class Program  
????{    
????public static void Main(string[] args)    
????{      
????Sum(new Number[]      
????{        
????new Integer32(1)?? // (1) initialization nightmares...        
????new Integer32(2)??         
????new Integer32(3)??        
????new Integer32(4)??        
????new Integer32(5)      
????});   
????}    
????public static Number Sum(Number[] array)    
????{      
????Number sum = array[0].GetZero(); // (2) instance-based factory methods are terrible design     
????for (int i = 0; i < array.Length; i++)        
????sum = sum.Add(array[i]);      
????return sum;    
????} 
????}  
????public interface Number  
????{    
????Number GetZero(); // (2) again... instance based factory methods are awful    
????Number Add(Number other);  
????}  
????public struct Integer32 : Number // (3) C# primitives cannot implement "Number"  
????{    
????int _value;    
????public Integer32(int value)    
????{      
????this._value = value;    
????}    
????Number Number.GetZero()    
????{      
????return new Integer32(0);    
????}     // (4) you will have to re-write these functions for every single type     
????Number Number.Add(Number other)    
????{      
????return new Integer32(_value + ((Integer32)other)._value);    
????}  
????}
????} // (5) this code is incredibly slow
???????????? EXAMPLE 3 ??????????????е?????????
?????????y????????????????a???
?????????ù???????????????????????????????????????????????????????????????????????????
??????????C#????????????“Number”???????????????????????????
???????????????????????????д?????????????
??????δ???????????????????????????
???????????????????? C# ???????????????????????????????????????????????????????????????? C# ??????????????????????????????????????????????????Щ??????е?????????????????? .Net ????й???????????????
???????/??й????????????????????????????????????з??????????д?????????????????????????????????????????????С?
????????????????????????????????????????????£????????????????????е??????????EXAMPLE 4?м???????
????/EXAMPLE 4 ----------------------------------------
????namespace ConsoleApplication
????{  
????public class Program  
????{    
????public static void Main(string[] args)    
????{     
????Sum<int>(new int[] { 1?? 2?? 3?? 4?? 5}?? new MathProvider_int());    
????}     // (1) all the methods need access to the provider    
????public static T
????Sum<T>(T[] array?? MathProvider<T> mathProvider)    
????{      
????T sum = mathProvider.GetZero();      
????for (int i = 0; i < array.Length; i++)        
????sum = mathProvider.Add(sum?? array[i]);      
????return sum;     }  
????public interface MathProvider<T>  
????{    
????T GetZero(); // (2) you still need instance factory methods    
????T Add(T left?? T right);  
????} 
????public class MathProvider_int : MathProvider<int>  
????{    
????public MathProvider_int() { }    
????int MathProvider<int>.GetZero()    
????{      
????return 0;    
????}     // (3) you still have to implement each function for every single type     
????int MathProvider<int>.Add(int left?? int right)    
????{      
????return left + right;    
????}  
????}
????} // (4) can be slow depending on implementation (this version is slow)
????EXAMPLE 4 ????????е????????????????????У???????????C#??????????????????????????????? EXMAPLE 3 ?е??????????????????????????????
???????з???????????? mathProvider ????????????д???????????????????????????????????????????????????
?????????????????????????????????????????????????????int???????
??????????????????????????????????????????????
?????????????????????? provider ???Щ”??????“???檔provider ???????????????????
?????????????????????????????????EXAMPLE 3?????? provider??EXAMPLE 4??????y?????y?????????????????????????????????????????????Щ?????????洢?????????????????????????????????????????????????
??????????…?? C# ?н???????????Ч??????????С?
???????????
?????? C# ??????????????????? System.Object ??????????????????????????????????????????????????????????????????????
????// EXAMPLE 5 ----------------------------------------
????namespace ConsoleApplication
????{  
????public class Program 
????{    
????public static void Main(string[] args)    
????{      
????MathProvider<int>.Sum(new int[] { 1?? 2?? 3?? 4?? 5});   
????}  
????}  
????public static class MathProvider<T> 
????{    
????public static T Sum(T[] array)    
????{       // (1) still requires a custom implementation for every type      
????if (typeof(T) == typeof(int))      
????{        
????T sum = (T)(object)0; // (2) largescale casting is very glitch prone        
????for (int i = 0; i < array.Length; i++)          
????sum = (T)(object)((int)(object)sum + ((int)(object)array[i]));        
????return sum;       }       t
????hrow new System.Exception("UNSUPPORTED TYPE"); // (3) runtime errors    
????}   } }
????// (4) horribly slow...
???????????????????????????á?????????????????á??????????????????????к??????
???????????????????????????????????
???????????д??????????????????????????????
?????????????????????????????
????????????
??????????????????????????? F?? ??????????????????? F?? ????????????????????????????????????????????????????Ц??????
????????????????????????????????????????á?
????????
????????……??????
????????????????????????????Ч??????????????????????й???????????????????ж?????????????????C#?????????????????????
?????????????????????????????????????ɡ????????????????????????????????????????????????????????????????Щ????????????????????
???????????????????У????????????????У???????????У????????????????????
????// EXAMPLE 5 ----------------------------------------
????namespace ConsoleApplication
????{  
????public class Program  
????{    
????public static void Main(string[] args)    
????{       // (1) requires a constructor method to be called prior to use      
????MathProviderConstructors.Construct();      
????MathProvider<int>.Sum(new int[] { 1?? 2?? 3?? 4?? 5});   
????}   }  
????public static class MathProviderConstructors  
????{    
????public static void Construct()    
????{      
????// (2) still requires a custom implementation for every type      
????MathProvider<int>.Sum = (int[] array) =>     
????{        
????int sum = 0;        
????for (int i = 0; i < array.Length; i++)          
????sum = sum + array[i];        
????return sum;      
????};     }   }  
????public static class MathProvider<T>  
????{    
????// (3) still have runtime errors for non-implemented types (null-ref)    
????public static System.Func<T[]?? T> Sum;  
????} }
????EXMAPLE 5 ????????????????????к?????????κ???????????????????????????????????????????á?????????????Щ覴?…(1)???????????????(2)?????????????????????????????(3)????????????????
?????????????????????????Щ??Э??????????????????????????????????????????????????????????? Visual Studio ????????????????????????????????μ?????????????????????????????????????????????????????????????????д???????????????н?????????????
????????
????????????????????????????汾??
????using Microsoft.CSharp; using System;
????using System.CodeDom.Compiler;
????using System.Reflection;
????namespace RuntimeCodeCompiling {  
????public static class Program  
????{    
????public static Action action;    
????public static void Main(string[] args)    
????{      
????Console.WriteLine("Sum(double): " + Generic_Math<double>.Sum(new double[] { 1?? 2?? 3?? 4?? 5 }));
????Console.WriteLine("Sum(int): " + Generic_Math<int>.Sum(new int[] { 1?? 2?? 3?? 4?? 5 }));      
????Console.WriteLine("Sum(decimal): " + Generic_Math<decimal>.Sum(new decimal[] { 1?? 2?? 3?? 4?? 5 }));      
????Console.ReadLine();    
????}    
????#region Generic Math Library Example    
????public static class Generic_Math<T>    
????{      
????public static Func<T[]?? T> Sum = (T[] array) =>      
????{ // This implementation will make this string be stored in memory during runtime??
????//so it might be better to read it from a file        
????string code = "(System.Func<NUMBER[]?? NUMBER>)((NUMBER[] array) =>
????{ NUMBER sum = 0; for (int i = 0; i < array.Length; i++) sum += array[i]; return sum; })";        
????// This requires that "T" has an implicit converter from int values and a "+" operator        
????code = code.Replace("NUMBER"?? typeof(T).ToString());        
????// This small of an example requires no namspaces or references        
????Generic_Math<T>.Sum = Generate.Object<Func<T[]?? T>>(new string[] { }?? new string[] { }?? code);
????return Generic_Math<T>.Sum(array);      
????};    
????}    
????/// <summary>Generates objects at runtime.</summary>    
????internal static class Generate   
????{
????/// <summary>Generates a generic object at runtime.</summary>      
????/// <typeparam name="T">The type of the generic object to create.</typeparam>     
????/// <param name="references">The required assembly references.</param>      
????/// <param name="name_spaces">The required namespaces.</param>      
????/// <param name="code">The object to generate.</param>      
????/// <returns>The generated object.</returns>      
????internal static T Object<T>(string[] references?? string[] name_spaces?? string code)      
????{
????string full_code = string.Empty;        
????if (name_spaces != null)          
????for (int i = 0; i < name_spaces.Length; i++)            
????full_code += "using " + name_spaces[i] + ";";        
????full_code += "namespace Seven.Generated
????{";        
????full_code += "public class Generator
????{";        
????full_code += "public static object Generate()
????{ return " + code + "; } } }";        
????CompilerParameters parameters = new CompilerParameters();        
????foreach (string reference in references)          
????parameters.ReferencedAssemblies.Add(reference);       
????parameters.GenerateInMemory = true;        
????CompilerResults results = new CSharpCodeProvider().CompileAssemblyFromSource(parameters?? full_code);        
????if (results.Errors.HasErrors)        
????{          
????string error = string.Empty;          
????foreach (CompilerError compiler_error in results.Errors)            
????error += compiler_error.ErrorText.ToString() + "/n";          
????throw new Exception(error);        
????}        
????MethodInfo generate = results.CompiledAssembly.GetType("Seven.Generated.Generator").GetMethod("Generate");        
????return (T)generate.Invoke(null?? null);      
????}    
????}   
????#endregion  
????} }
??????????????
?????????????????洢?????????????????? string hacking??????macros aka string replace?????????????????????????д?????????????????ú???????ú?????????????????????????????б??????????????????ú?????
?????????ε??ú????????????????????????????????????????????????????????????????????????????蹹???????ɡ?
??????????????????????????????????????????????????????????????????????????????????????????????????????????????л?????????????????????????????????????????????????????????????
???????
?????????????????????????汾??
????????й??????????÷??????????????????????????????????????
???????????????????????????????е??????
????С???????Щ“???”????????
????1.??????????????д???????????????????????? ????????????????????????д????????????????????????????????????棬?????????????????? Visual Studio ?????????C???????
????2.???????????????????????????????????????????????????????????????????????????????????????? Mono ????????????????????????????????“?????”???????????
????3.????????“typeof(T).ToString()”??????????У????????Щ????????????????????????????????????? ?????????????????????????????????????????????????
????4.????????б?????????????????????????”struct Fraction128“????????”+“?????????????????????????????????????????????????д??? VS ???????????????????????????????????????????????????????????Щ??????????????????????????????????????Щ?????????????????£?:P
????????
???????????????????????????????????????????????????????????????????????????Щ?????????????????????????????????????н?????????????????????????????????
??????????????…??????????????Щ?????????????????κν??飬????????????????лл??