https://github.com/jacraig/fastactivator
It's Activator.CreateInstance... But faster.
https://github.com/jacraig/fastactivator
activator createinstance reflection
Last synced: 7 months ago
JSON representation
It's Activator.CreateInstance... But faster.
- Host: GitHub
- URL: https://github.com/jacraig/fastactivator
- Owner: JaCraig
- License: apache-2.0
- Created: 2020-04-15T12:50:25.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-10-29T09:57:10.000Z (over 1 year ago)
- Last Synced: 2024-10-29T11:55:50.331Z (over 1 year ago)
- Topics: activator, createinstance, reflection
- Language: C#
- Homepage: https://jacraig.github.io/FastActivator/
- Size: 23.8 MB
- Stars: 5
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
README
# FastActivator
[](https://github.com/JaCraig/FastActivator/actions/workflows/dotnet-publish.yml) [](https://coveralls.io/github/JaCraig/FastActivator?branch=master)
FastActivator is a C# library that provides a faster alternative to the `System.Activator` class for creating instances of classes.
## Purpose
The purpose of FastActivator is to replace the usage of `System.Activator` with a more efficient implementation, resulting in improved performance when creating instances of classes.
## Speed Comparisons
| Method | Mean | Error | StdDev | Rank | Gen0 | Allocated |
|---------------------------- |----------:|---------:|---------:|-----:|-------:|----------:|
| ActivatorCreateInstance | 182.80 ns | 1.272 ns | 1.127 ns | 2 | 0.0401 | 336 B |
| FastActivatorCreateInstance | 28.27 ns | 0.422 ns | 0.352 ns | 1 | 0.0115 | 96 B |
In many instances it's about 2x to 10x faster depending on the method used.
## Usage
To use FastActivator, follow these steps:
1. Add a reference to the FastActivator library in your project.
```ps
dotnet add package Fast.Activator
```
2. Import the required namespaces:
```csharp
using Fast.Activator;
```
3. To create an instance of a class using FastActivator, use one of the following methods:
- `CreateInstance(params object[] args)`: Creates an instance of the specified class `TClass` with the provided arguments, if any.
```csharp
var instance = FastActivator.CreateInstance(arg1, arg2);
```
- `CreateInstance()`: Creates an instance of the specified class `TClass` without any arguments.
```csharp
var instance = FastActivator.CreateInstance();
```
- `CreateInstance(Type type, params object[] args)`: Creates an instance of the specified `Type` with the provided arguments, if any.
```csharp
var instance = FastActivator.CreateInstance(typeof(MyClass), arg1, arg2);
```
- `CreateInstance(Type type)`: Creates an instance of the specified `Type` without any arguments.
```csharp
var instance = FastActivator.CreateInstance(typeof(MyClass));
```
Note: If the object cannot be created, the methods will return `null`.
## Contributing
Contributions to FastActivator are welcome! If you would like to contribute, please follow these guidelines:
- Fork the repository and create a new branch for your feature or bug fix.
- Make your changes and ensure that the tests pass.
- Submit a pull request describing your changes and the problem they solve.
- Ensure that your code adheres to the project's coding conventions and style guidelines.
## License
FastActivator is released under the [Apache 2.0 License](https://github.com/JaCraig/FastActivator/blob/master/LICENSE).