This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var rect = new Rectangle(10, 5); | |
var widerRect = rect with { Width = 20 }; | |
var longerRect = widerRect with { Height = 10 }; | |
Console.WriteLine(rect.Equals(widerRect)); // False | |
Console.WriteLine(widerRect.Equals(longerRect)); // False | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated Rectangle class omitted | |
void main() | |
{ | |
Rectangle rectangle = new Rectangle(10.0, 5.0); | |
Rectangle rectangle2 = rectangle.<>Clone(); | |
rectangle2.Width = 20.0; | |
Rectangle rectangle3 = rectangle2; | |
Rectangle rectangle4 = rectangle3.<>Clone(); | |
rectangle4.Height = 10.0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated Rectangle class omitted | |
void main() | |
{ | |
Rectangle rectangle = new Rectangle(10.0, 5.0); | |
Rectangle rectangle2 = rectangle.<>Clone(); | |
rectangle2.Width = 20.0; | |
Rectangle rectangle3 = rectangle2; | |
Rectangle rectangle4 = rectangle3.<>Clone(); | |
rectangle4.Height = 10.0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BlogPost | |
{ | |
public int Id { get; set; } | |
public string Title { get; set; } | |
public string Description { get; set; } | |
public bool Disabled { get; set; } | |
public DateTime Created { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BlogPost | |
{ | |
public int Id { get; set; } | |
public string Title { get; set; } | |
public string Description { get; set; } | |
public bool Disabled { get; set; } | |
public DateTime Created { get; set; } | |
} | |
public interface IBlogService |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoggingInterceptor : IInterceptor | |
{ | |
public void Intercept(IInvocation invocation) | |
{ | |
Console.WriteLine($"Calling method {invocation.TargetType}.{invocation.Method.Name}."); | |
invocation.Proceed(); // continue | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var generator = new ProxyGenerator(); | |
var actual = new BlogService(); | |
var proxiedService = (IBlogService)proxyGenerator.CreateInterfaceProxyWithTarget(typeof(IBlogService), actual, new LoggingInterceptor()); | |
// Use proxiedService as you would normally use an IBlogService instance |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoggingInterceptor : IInterceptor | |
{ | |
private readonly ILogger<LoggingInterceptor> _logger; | |
public LoggingInterceptor(ILogger<LoggingInterceptor> logger) | |
{ | |
_logger = logger; | |
} | |
public void Intercept(IInvocation invocation) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.AddSingleton(new ProxyGenerator()); | |
services.AddScoped<IInterceptor, LoggingInterceptor>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ServicesExtensions | |
{ | |
public static void AddProxiedScoped<TInterface, TImplementation>(this IServiceCollection services) | |
where TInterface : class | |
where TImplementation : class, TInterface | |
{ | |
services.AddScoped<TImplementation>(); | |
services.AddScoped(typeof(TInterface), serviceProvider => | |
{ | |
var proxyGenerator = serviceProvider.GetRequiredService<ProxyGenerator>(); |