fast-member

repository·master·Indexed 22 days ago

https://github.com/mgravell/fast-member

A .NET library providing high-performance access to fields and properties to bypass standard reflection overhead. It includes TypeAccessor and ObjectAccessor for runtime member access, and ObjectReader for converting IEnumerable<T> sequences to DataTable or streaming data via SqlBulkCopy. Note: Not compatible with AOT environments such as Xamarin iOS and Unity IL2CPP due to runtime IL emission.

Tokens
678
Snippets
4
Records
5
Agent score
28%

What's inside fast-member

  1. Convert a sequence of objects to a DataTable using ObjectReader

    master

    You can easily load a DataTable from an IEnumerable<T> by using ObjectReader.Create(data). The ObjectReader implements IDataReader, making it compatible with standard ADO.NET methods like DataTable.Load().

    IEnumerable<SomeType> data = ... 
    var table = new DataTable(); 
    using(var reader = ObjectReader.Create(data)) 
    {
      table.Load(reader);
    }
  2. Perform high-speed bulk copies using ObjectReader and SqlBulkCopy

    master

    To stream data from a collection of objects directly into a database using SqlBulkCopy, use ObjectReader.Create and specify the exact member names you wish to include in the reader. This provides a very fast path for data ingestion.

    using(var bcp = new SqlBulkCopy(connection)) 
    using(var reader = ObjectReader.Create(data, "Id", "Name", "Description")) 
    {
      bcp.DestinationTableName = "SomeTable"; 
      bcp.WriteToServer(reader);
    }
  3. Access members of a specific object instance with ObjectAccessor

    master

    Use ObjectAccessor.Create(obj) to create an accessor for a specific object instance. This is useful when the object might be static or a DLR type. You can then access its members using the indexer with the property name.

    // obj could be static or DLR 
    var wrapped = ObjectAccessor.Create(obj);
    string propName = // something known only at runtime 
    Console.WriteLine(wrapped[propName]);
  4. Access members of an arbitrary type with TypeAccessor

    master

    Use TypeAccessor.Create(type) to create an accessor for a specific type when member names are only known at runtime. This allows for high-performance reading and writing of fields or properties using an indexer with the object instance and the member name.

    Note: This is particularly useful for accessing members of DLR types where standard reflection is difficult.

    var accessor = TypeAccessor.Create(type); 
    string propName = // something known only at runtime 
    while( /* some loop of data */ )
    {
      accessor[obj, propName] = rowValue; 
    }