Core
Castle Core, including Castle DynamicProxy, Logging Services and DictionaryAdapter
Top Related Projects
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
An addictive .NET IoC container
This repository contains all relevant information about Unity Container suit
An easy, flexible, and fast Dependency Injection library that promotes best practice to steer developers towards the pit of success.
the ninja of .net dependency injectors
Quick Overview
Castle Core is a lightweight, open-source library for .NET that provides a set of tools and utilities for aspect-oriented programming, dynamic proxy generation, and inversion of control. It serves as a foundation for other Castle projects and can be used independently in various .NET applications to enhance flexibility and modularity.
Pros
- Powerful dynamic proxy generation capabilities
- Extensive support for aspect-oriented programming
- Lightweight and easy to integrate into existing projects
- Well-maintained and actively developed
Cons
- Learning curve for developers new to aspect-oriented programming
- Limited documentation for some advanced features
- Potential performance overhead in certain scenarios
- May introduce complexity in larger projects if not used judiciously
Code Examples
- Creating a simple dynamic proxy:
using Castle.DynamicProxy;
public interface IMyService
{
void DoSomething();
}
public class MyInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine("Before method call");
invocation.Proceed();
Console.WriteLine("After method call");
}
}
var generator = new ProxyGenerator();
var proxy = generator.CreateInterfaceProxyWithoutTarget<IMyService>(new MyInterceptor());
proxy.DoSomething();
- Using attribute-based interception:
using Castle.DynamicProxy;
public class LogAttribute : InterceptorAttribute
{
public override void Intercept(IInvocation invocation)
{
Console.WriteLine($"Calling method: {invocation.Method.Name}");
invocation.Proceed();
}
}
public interface IMyService
{
[Log]
void DoSomething();
}
var generator = new ProxyGenerator();
var proxy = generator.CreateInterfaceProxyWithoutTarget<IMyService>();
proxy.DoSomething();
- Implementing property interception:
using Castle.DynamicProxy;
public class PropertyInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
if (invocation.Method.Name.StartsWith("set_"))
{
Console.WriteLine($"Setting property: {invocation.Method.Name.Substring(4)}");
}
invocation.Proceed();
}
}
public interface IMyClass
{
string MyProperty { get; set; }
}
var generator = new ProxyGenerator();
var proxy = generator.CreateInterfaceProxyWithoutTarget<IMyClass>(new PropertyInterceptor());
proxy.MyProperty = "Hello, World!";
Getting Started
To get started with Castle Core, follow these steps:
-
Install the NuGet package:
dotnet add package Castle.Core
-
Add the necessary using statements to your code:
using Castle.DynamicProxy;
-
Create a proxy generator and define your interceptors:
var generator = new ProxyGenerator(); var interceptor = new YourCustomInterceptor(); var proxy = generator.CreateInterfaceProxyWithoutTarget<IYourInterface>(interceptor);
-
Use the proxy object to invoke methods and apply interception logic.
Competitor Comparisons
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
Pros of runtime
- Larger, more active community with frequent updates and contributions
- Comprehensive coverage of .NET runtime and core libraries
- Official Microsoft support and integration with .NET ecosystem
Cons of runtime
- More complex codebase due to its extensive scope
- Steeper learning curve for contributors
- Potentially slower to implement specific features due to rigorous review process
Code Comparison
Core:
public class Interceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
// Custom interception logic
}
}
runtime:
public class RuntimeMethodHandle
{
internal IntPtr Value;
public RuntimeMethodHandle(IntPtr v)
{
Value = v;
}
}
Summary
Core focuses on providing a lightweight, flexible framework for aspect-oriented programming and dependency injection in .NET. It offers simplicity and ease of use for specific scenarios.
runtime, on the other hand, is the official .NET runtime implementation, offering a comprehensive set of libraries and tools for .NET development. It provides a more extensive feature set but comes with increased complexity.
The choice between the two depends on the specific needs of your project, with Core being more suitable for targeted AOP and DI scenarios, while runtime is essential for broader .NET development and runtime functionality.
An addictive .NET IoC container
Pros of Autofac
- More active development and maintenance
- Extensive documentation and community support
- Better performance in large-scale applications
Cons of Autofac
- Steeper learning curve for beginners
- More complex configuration for advanced scenarios
- Slightly larger memory footprint
Code Comparison
Autofac registration:
var builder = new ContainerBuilder();
builder.RegisterType<MyService>().As<IService>();
var container = builder.Build();
Castle Windsor registration:
var container = new WindsorContainer();
container.Register(Component.For<IService>().ImplementedBy<MyService>());
Both Autofac and Core (Castle Windsor) are powerful dependency injection containers for .NET applications. Autofac offers more flexibility and features, making it suitable for complex projects. Core, on the other hand, provides a simpler API and is easier to get started with for smaller applications.
Autofac has gained popularity due to its extensive documentation, active community, and regular updates. It also performs better in large-scale applications with numerous dependencies. However, this comes at the cost of a steeper learning curve and more complex configuration for advanced scenarios.
Core (Castle Windsor) offers a more straightforward approach to dependency injection, making it easier for beginners to understand and implement. It has a smaller memory footprint and simpler configuration, but may lack some of the advanced features found in Autofac.
This repository contains all relevant information about Unity Container suit
Pros of Unity
- More active development and maintenance
- Broader range of container features and configuration options
- Better integration with ASP.NET Core and other Microsoft technologies
Cons of Unity
- Steeper learning curve for beginners
- More complex configuration for advanced scenarios
- Potentially slower performance in some use cases
Code Comparison
Unity:
IUnityContainer container = new UnityContainer();
container.RegisterType<IService, ServiceImplementation>();
var service = container.Resolve<IService>();
Castle Windsor:
var container = new WindsorContainer();
container.Register(Component.For<IService>().ImplementedBy<ServiceImplementation>());
var service = container.Resolve<IService>();
Both Unity and Castle Windsor are popular dependency injection containers for .NET applications. Unity offers more features and better integration with Microsoft technologies, while Castle Windsor is known for its simplicity and ease of use. Unity's configuration can be more verbose, but it provides greater flexibility. Castle Windsor has a more straightforward API, making it easier for beginners to grasp. Performance-wise, Castle Windsor may have a slight edge in some scenarios, but Unity has improved significantly in recent versions. The choice between the two often depends on specific project requirements and team preferences.
An easy, flexible, and fast Dependency Injection library that promotes best practice to steer developers towards the pit of success.
Pros of SimpleInjector
- Faster performance and lower memory footprint
- Simpler API with less configuration required
- Better support for advanced scenarios like decorators and interception
Cons of SimpleInjector
- Less mature ecosystem and community support
- Fewer built-in features compared to Castle Windsor
- Steeper learning curve for complex dependency graphs
Code Comparison
SimpleInjector:
var container = new Container();
container.Register<IUserRepository, SqlUserRepository>();
container.Register<IUserService, UserService>();
var userService = container.GetInstance<IUserService>();
Castle Windsor:
var container = new WindsorContainer();
container.Register(Component.For<IUserRepository>().ImplementedBy<SqlUserRepository>());
container.Register(Component.For<IUserService>().ImplementedBy<UserService>());
var userService = container.Resolve<IUserService>();
Both SimpleInjector and Core (Castle Windsor) are popular dependency injection containers for .NET applications. SimpleInjector focuses on simplicity and performance, while Castle Windsor offers a more feature-rich and flexible approach. The choice between them often depends on the specific requirements of the project and the developer's preferences.
SimpleInjector's streamlined API makes it easier to set up and use for straightforward scenarios, but it may require more effort for complex configurations. Castle Windsor, on the other hand, provides more built-in features and a mature ecosystem, which can be beneficial for larger projects with diverse dependency injection needs.
the ninja of .net dependency injectors
Pros of Ninject
- Lightweight and fast dependency injection container
- Easy to use with a fluent API and straightforward configuration
- Extensive documentation and active community support
Cons of Ninject
- Limited features compared to more comprehensive IoC containers
- Performance can degrade with complex object graphs
- Less suitable for large-scale enterprise applications
Code Comparison
Ninject:
var kernel = new StandardKernel();
kernel.Bind<IWeapon>().To<Sword>();
var warrior = kernel.Get<Samurai>();
Castle Windsor:
var container = new WindsorContainer();
container.Register(Component.For<IWeapon>().ImplementedBy<Sword>());
var warrior = container.Resolve<Samurai>();
Summary
Ninject is a lightweight and user-friendly dependency injection framework, ideal for small to medium-sized projects. It offers a simple API and good documentation. However, it may lack some advanced features and performance optimizations found in more comprehensive IoC containers like Castle Windsor.
Castle Windsor, part of the Castle Project Core, provides a more robust set of features and is better suited for larger, enterprise-level applications. It offers better performance for complex object graphs but may have a steeper learning curve compared to Ninject.
Choose Ninject for simplicity and ease of use in smaller projects, while Castle Windsor is preferable for larger, more complex applications requiring advanced IoC features and optimized performance.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Castle Core

Castle Core provides common Castle Project abstractions including logging services. It also features Castle DynamicProxy a lightweight runtime proxy generator, and Castle DictionaryAdapter.
See the documentation.
Releases
See the Releases.
Debugging symbols are available in symbol packages in the AppVeyor build artifacts since version 4.1.0. For example, here are the artifacts for 4.1.0.
License
Castle Core is © 2004-2022 Castle Project. It is free software, and may be redistributed under the terms of the Apache 2.0 license.
Contributing
Browse the contributing section of our Home repository to get involved.
Building
Platforms | NuGet Feed |
---|---|
Windows & Linux | Preview Feed |
On Windows
build.cmd
Compilation requires a C# 9 compiler, an up-to-date .NET Core SDK, and MSBuild 15+ (which should be included in the former).
Running the unit tests additionally requires the .NET Framework 4.6.2+ as well as the .NET Core 2.1, 3.1 and 6.0 runtimes to be installed. (If you do not have all of those installed, you can run the tests for a specific target framework using dotnet test -f <framework>
.)
These requirements should be covered by Visual Studio 2022 and the .NET 6 SDK.
On Linux
./build.sh
Compilation requires an up-to-date .NET Core SDK.
Running the unit tests additionally requires the .NET Core 3.1 and 6.0 runtimes to be installed, as well as either Docker or Mono. For the latter, we recommend Mono 5.10+, though older versions (4.6.1+) might still work as well.
:information_source: Mono runtime support: Castle Core runs with minor limitations and defects on Mono 4.0.2+ (however 4.6.1+ is highly recommended, or 5.10+ if your code uses new C# 7.x language features such as in
parameters).
We test against up-to-date Mono versions in order to fix known defects as soon as possible. Because of this, if you are using an older Mono version than our Continuous Integration (CI) build, you might see some unit tests fail.
For known Mono defects, check our issue tracker, as well as unit tests marked with [Platform(Exclude = "Mono", ...)]
in the source code.
Conditional Compilation Symbols
The following conditional compilation symbols (vertical) are currently defined for each of the build configurations (horizontal):
Symbol | .NET 4.6.2 | .NET Standard 2.x and .NET 6 |
---|---|---|
FEATURE_APPDOMAIN | :white_check_mark: | :no_entry_sign: |
FEATURE_ASSEMBLYBUILDER_SAVE | :white_check_mark: | :no_entry_sign: |
FEATURE_SERIALIZATION | :white_check_mark: | :no_entry_sign: |
FEATURE_SYSTEM_CONFIGURATION | :white_check_mark: | :no_entry_sign: |
FEATURE_APPDOMAIN
- enables support for features that make use of an AppDomain in the host.FEATURE_ASSEMBLYBUILDER_SAVE
- enabled support for saving the dynamically generated proxy assembly.FEATURE_SERIALIZATION
- enables support for serialization of dynamic proxies and other types.FEATURE_SYSTEM_CONFIGURATION
- enables features that use System.Configuration and the ConfigurationManager.
Top Related Projects
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
An addictive .NET IoC container
This repository contains all relevant information about Unity Container suit
An easy, flexible, and fast Dependency Injection library that promotes best practice to steer developers towards the pit of success.
the ninja of .net dependency injectors
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot