All Projects → KeRNeLith → ImmediateReflection

KeRNeLith / ImmediateReflection

Licence: MIT license
.NET library that aims to provide faster usage of C# reflection features. Especially the usage of constructor and members accessors (get/set). It also provides an ObjectWrapper object allowing to use Reflection in a faster way. And finally it offers the possibility to create strongly typed delegates.

Programming Languages

C#
18002 projects
powershell
5483 projects

Projects that are alternatives of or similar to ImmediateReflection

Signals.jl
Multi-Paradigm Dynamic Fast Functional Reactive Programming in Julia
Stars: ✭ 37 (+23.33%)
Mutual labels:  fast, dynamic
tfvars-annotations
[not-WIP] Update values in terraform.tfvars using annotations
Stars: ✭ 20 (-33.33%)
Mutual labels:  dynamic
ODYM
Open Dynamic Material Systems Model
Stars: ✭ 36 (+20%)
Mutual labels:  dynamic
Rails-4-ElasticSearch-dynamic-facets
Rails 4 ElasticSearch integration with dynamic facets
Stars: ✭ 16 (-46.67%)
Mutual labels:  dynamic
DynamicComponents-AI2
An App Inventor extension to add full support for creating any type of component at runtime, in your app.
Stars: ✭ 47 (+56.67%)
Mutual labels:  dynamic
FastHttpClient
封装OkHttp3,对外提供了POST请求、GET请求、上传文件、下载文件、https请求、cookie管理等功能
Stars: ✭ 60 (+100%)
Mutual labels:  fast
stone paper scissor defeator using opencv keras
In this repository i tried to replicate a cool project by a japanese scientist who made a machine which had 100 % accuracy in defeating humans in the game of stone-paper and scissors
Stars: ✭ 22 (-26.67%)
Mutual labels:  fast
TypeKitchen
TypeKitchen is a set of small libraries for fast metaprogramming in .NET Standard.
Stars: ✭ 14 (-53.33%)
Mutual labels:  fast
better-serializer
General serializer for PHP. An alternative to JmsSerializer.
Stars: ✭ 27 (-10%)
Mutual labels:  fast
wotpp
A small macro language for producing and manipulating strings.
Stars: ✭ 75 (+150%)
Mutual labels:  fast
XDP-Firewall
An XDP firewall that is capable of filtering specific packets based off of filtering rules specified in a config file. IPv6 is supported!
Stars: ✭ 129 (+330%)
Mutual labels:  fast
dynamic-datasource-starter
springboot 动态切换数据的基本思想与实现方法
Stars: ✭ 12 (-60%)
Mutual labels:  dynamic
framework
Cygnite PHP Framework- A Modern Toolkit For Web Developers
Stars: ✭ 43 (+43.33%)
Mutual labels:  fast
hash-wasm
Lightning fast hash functions using hand-tuned WebAssembly binaries
Stars: ✭ 382 (+1173.33%)
Mutual labels:  fast
ormsgpack
Msgpack serialization/deserialization library for Python, written in Rust using PyO3 and rust-msgpack. Reboot of orjson. msgpack.org[Python]
Stars: ✭ 88 (+193.33%)
Mutual labels:  fast
simple json
Simple way to dynamically convert from and to JSON using build-time generators given a type.
Stars: ✭ 15 (-50%)
Mutual labels:  dynamic
DuktapeJava
Tiny Powerfull JavaScript Engine On Android Platform integrating with java
Stars: ✭ 74 (+146.67%)
Mutual labels:  dynamic
gologger
A concurrent, fast queue/service worker based filesystem logging system perfect for servers with concurrent connections
Stars: ✭ 16 (-46.67%)
Mutual labels:  fast
Meh
Python configuration files in Python. ¯\_(ツ)_/¯
Stars: ✭ 20 (-33.33%)
Mutual labels:  dynamic
anchor
Create Dynamic CLI's as your GitOps Marketplace
Stars: ✭ 38 (+26.67%)
Mutual labels:  dynamic
Build AppVeyor Build Status
Coverage Coveralls Coverage Status SonarQube SonarCloud Coverage
Quality Quality Gate
Nuget Nuget status
License GitHub license

ImmediateReflection

What is ImmediateReflection?

This is .NET library that aims to provide a faster usage of C# reflection features. Especially the usage of constructor, members accessors (get/set) and attributes.

It provides these features while trying to keep an API as similar as the standard Reflection API (Fully documented and ReSharper compliant).

To see how powerful the library is you can consult some benchmarks there.

The library is highly tested to cover as much as possible real cases, because using Reflection is some kind of core code and must be reliable to build on it.

Getting started

See the library documentation.

Getting a type

The library is pretty simple to use, it has wrappers of standard Type, FieldInfo and PropertyInfo that are respectively called ImmediateType, ImmediateField and ImmediateProperty.

The get access to fields and properties it is like the standard way, you get access to a Type and then request its fields and properties. The entry point of the library is the TypeAccessor.

See following examples:

ImmediateType type = TypeAccessor.Get(typeof(MySuperType));

// or

ImmediateType type = TypeAccessor.Get<MySuperType>();

Note that there are other access methods that allow to get an ImmediateType with non public member or by specifying BindingFlags.

ImmediateType type = TypeAccessor.Get<MySuperType>(includeNonPublicMembers: true);

// or

// Flags allow to get a type with member that fulfill requested flags
ImmediateType type = TypeAccessor.Get<MySuperType>(BindingFlags.Public | BindingFlags.Static);

Note: There is a built-in cache behind the TypeAccessor.

Instantiate a type

The ImmediateType allows to instantiate types via their default constructor if available. This feature is faster than making a traditional call to Activator.CreateInstance(Type).

Here is a quick example:

ImmediateType type = TypeAccessor.Get<MySuperType>();

// Create a new instance of MySuperType
object newInstance = type.New();

// You can also use the version that not throws in case of failure
bool succeed = type.TryNew(out object instance, out Exception _);

Copy an instance

The ImmediateType allows to create a copy of a given instance via a copy constructor if available. This feature is faster than making a traditional call to Activator.CreateInstance(Type, Instance).

Here is a quick example:

ImmediateType type = TypeAccessor.Get<MySuperType>();

MySuperType instance = new MySuperType
{
    TestProperty = 12
};

// Create a copy instance of MySuperType
object newInstance = type.Copy(instance);

// You can also use the version that not throws in case of failure
bool succeed = type.TryCopy(instance, out object newInstance, out Exception _);

Note also that a more easy way of using copy is available as extension directly when manipulating an instance.

MySuperType instance = new MySuperType
{
    TestProperty = 12
};

// Create a copy instance of MySuperType
MySuperType newInstance = instance.Copy();

Obviously in such situation you would have directly called the copy constructor of MySuperType, but we have to keep in mind that it is designed to be use when the instance we manipulate has not been created in such explicit way.

Getting a field or a property

ImmediateType type = TypeAccessor.Get<MySuperType>();

// For fields
ImmediateField field = type.GetField("FieldName");
// or
ImmediateField field = type.Fields["FieldName"];
// There is also type.GetFields()

// For properties
ImmediateProperty property = type.GetProperty("PropertyName");
// or
ImmediateProperty property = type.Properties["PropertyName"];
// There is also type.GetProperties()

// For all members
IEnumerable<ImmediateMember> members = type.Members;
// or
IEnumerable<ImmediateMember> members = type.GetMembers();

// For a member
ImmediateMember member = type.GetMember("MemberName");
// or
ImmediateMember member = type["MemberName"];

When you have type wrapping a field or a property you are able to get or set it like in a standard way.

object instance = new MySuperType();

ImmediateProperty property = type.GetProperty("PropertyName");

// Get
object propertyValue = property.GetValue(instance);

// Set
property.SetValue(instance, "New Value");

To let the user of the library access eventual missing functionalities, each wrapping type from ImmediateReflection gives an access to the equivalent standard structure.

ImmediateProperty property = type.GetProperty("PropertyName");

PropertyInfo propertyInfo = property.PropertyInfo;

Getting attributes

Both ImmediateType, ImmediateField and ImmediateProperty inherit from ImmediateMember which provide an API to check/get attributes that are applied respectively to a type, field or a property.

All methods are accessible in their templated and not templated versions. Following some examples of accessible methods:

ImmediateType type = ...;
bool hasAttribute = type.IsDefined<MyAttribute>();

ImmediateField field = ...;
MyAttribute attribute = field.GetAttribute<MyAttribute>(inherit: true);

ImmediateProperty property = ...;
IEnumerable<Attribute> attributes = property.GetAttributes(typeof(MyAttribute));

IEnumerable<Attribute> attributes = type.GetAllAttributes(inherit: true);

It is also possible to directly retrieve attributes of a given MemberInfo from the built in cache.

PropertyInfo property = ...;
bool hasAttribute = property.IsDefinedImmediateAttribute<MyAttribute>();

FieldInfo field = ...;
MyAttribute attribute = field.GetImmediateAttribute<MyAttribute>();

Object wrapper

By using ImmediateType API you can manipulate get/set on object via "open" methods, meaning you can specify the instance on which applying the method.

ImmediateReflection also provides an ObjectWrapper that does the same job as ImmediateType but on a "closed" way. It means that get/set will be applied only on the wrapped instance.

Following a quick example:

MyClass myObject = new MyClass();

ObjectWrapper wrapper = new ObjectWrapper(myObject);

// Properties/Fields
ImmediateField field = wrapper.GetField("_myField");
ImmediateProperty property = wrapper.GetProperty("MyProperty");

// Get
object propertyValue = wrapper.GetPropertyValue("MyProperty");

// Set
wrapper.SetPropertyValue("MyOtherProperty", 42);    // myObject.MyOtherProperty = 42

Note that the wrapper gives access to the wrapped object, its Type, ImmediateType and public members.

Creating typed delegate (Open delegate)

ImmediateReflection provides an API like standard one for Type, FieldInfo and PropertyInfo, this means get/set for properties use object both for target and parameter/return type.

But in some cases you know the type owning a property, or better the type of the property too.

To answer these cases ImmediateReflection provides extensions to PropertyInfo that allow you to create strongly typed delegates for an even faster get/set of properties.

See some of the following examples:

class MyType
{
    int MyProperty { get; set; }

    string MyStringProperty { get; set; }
}

PropertyInfo myProperty = typeof(MyType).GetProperty(nameof(MyType.MyProperty));
GetterDelegate<MyType, int> getter = myProperty.CreateGetter<MyType, int>();

// Notice that this method can throw if passing invalid types
// There is also a try version
bool succeed = myProperty.TryCreateGetter(out GetterDelegate<MyType, int> getter);

// Then you can use this getter simply like this
MyType myObject = new MyType { MyProperty = 12 };
int value = getter(myObject);  // 12


// Note that the same exists for setter
PropertyInfo myStringProperty = typeof(MyType).GetProperty(nameof(MyType.MyStringProperty));
SetterDelegate<MyType, string> setter = myProperty.CreateSetter<MyType, string>();
// Or
bool succeed = myProperty.TryCreateSetter(out SetterDelegate<MyType, string> setter);

// Then you can use this getter simply like this
MyType myObject = new MyType { MyStringProperty = "Init" };
setter(myObject, "New value");  // Sets myObject.MyStringProperty to "New value"

If you only knows the owner type then you can use the alternative version of these delegate helpers that will use object for the property value.

PropertyInfo myProperty = typeof(MyType).GetProperty(nameof(MyType.MyProperty));
GetterDelegate<MyType> getter = myProperty.CreateGetter<MyType>();

// Notice that this method can throw if passing invalid types
// There is also a try version
bool succeed = myProperty.TryCreateGetter(out GetterDelegate<MyType> getter);

// Then you can use this getter simply like this
MyType myObject = new MyType { MyProperty = 12 };
object value = getter(myObject);  // 12 wrapped in an object


// Note that the same exists for setter
PropertyInfo myStringProperty = typeof(MyType).GetProperty(nameof(MyType.MyStringProperty));
SetterDelegate<MyType> setter = myProperty.CreateSetter<MyType>();
// Or
bool succeed = myProperty.TryCreateSetter(out SetterDelegate<MyType> setter);

// Then you can use this getter simply like this
MyType myObject = new MyType { MyStringProperty = "Init" };
setter(myObject, "New value");  // Sets myObject.MyStringProperty to "New value"

You can then stores these delegate to boost your reflection get/set over properties.

Extensions

The library also provides some extensions for standard types to easily get Immediate Reflection types.

Like those:

Type myType = ...;

ImmediateType myImmediateType = myType.GetImmediateType();

Targets

  • .NET Standard
  • .NET Core
  • .NET Framework

Supports Source Link


Dependencies

For targets higher than .NET Standard 2.0:

  • System.Reflection.Emit.LightWeight

Notes

  • It uses NUnit3 for unit testing (not published).

  • The library code is published annotated with JetBrains annotations.


Installation

ImmediateReflection is available on NuGet

PM> Install-Package ImmediateReflection

Maintainer(s)

Contributor(s)

This project exists thanks to all the people who have contributed to the code base.


Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].