Advertisement
uniblab

Loading the intrinsic operators from embedded resource

Dec 5th, 2020 (edited)
1,629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. private static System.Collections.Generic.IDictionary<System.String, Type> GetInitialDictionary( System.IO.TextReader file ) {
  2.     var output = new System.Collections.Generic.Dictionary<System.String, Type>( System.StringComparer.Ordinal );
  3.  
  4.     var headers = new System.Collections.Generic.Dictionary<System.String, System.Int32>( System.StringComparer.OrdinalIgnoreCase );
  5.     System.Int32 i = 0;
  6.     foreach ( var header in file.ReadRecord( "\r\n", '\"', ',' ) ) {
  7.         headers.Add( header, i++ );
  8.     }
  9.     while ( -1 != file.Peek() ) {
  10.         var row = file.ReadRecord( "\r\n", '\"', ',' ).ToArray();
  11.         if ( row.Length <= 3 ) {
  12.             continue;
  13.         }
  14.         var assembly = System.Reflection.Assembly.Load( row[ headers[ "Assembly" ] ] );
  15.         var type = assembly.GetType( row[ headers[ "Type" ] ], true, true );
  16.         var method = type.GetMethod(
  17.             row[ headers[ "Method" ] ],
  18.             0,
  19.             System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public,
  20.             null,
  21.             System.Reflection.CallingConventions.Standard,
  22.             new System.Type[ 1 ] { typeof( Icod.Collections.Immutable.IStack<Type> ) },
  23.             null
  24.         );
  25.         if ( method.ReturnType != typeof( Icod.Collections.Immutable.IStack<Type> ) ) {
  26.             throw new System.InvalidOperationException( "Intrinsic operator misconfigured" );
  27.         }
  28.         var code = method.CreateDelegate(
  29.             typeof( System.Func<Icod.Collections.Immutable.IStack<Type>, Icod.Collections.Immutable.IStack<Type>> )
  30.         );
  31.         var name = row[ headers[ "Name" ] ];
  32.         var @operator = new Type(
  33.             name,
  34.             row[ headers[ "Ancestry" ] ],
  35.             null,
  36.             (System.Func<Icod.Collections.Immutable.IStack<Type>, Icod.Collections.Immutable.IStack<Type>>)code
  37.         );
  38.         output.Add( name, @operator );
  39.     }
  40.  
  41.     return output;
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement