public record SomeRecord(string Value)
{
public string GetValue()
{
return Value;
}
}public class HookFunctionAndPrint : OverrideMethodAspect
{
public override dynamic? OverrideMethod()
{
//hooks on method start
var functionName = meta.Target.Method.Name;
Console.WriteLine($"Hooked on {functionName} method start");
try
{
// Let the method do its own thing.
return meta.Proceed();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
return null;
}
finally
{
Console.WriteLine($"Hooked on {functionName} method end");
}
}
}public class HookFunctionAndPrint : OverrideMethodAspect
{
public override dynamic? OverrideMethod()
{
//hooks on method start
var functionName = meta.Target.Method.Name;
Console.WriteLine($"Hooked on {functionName} method start");
try
{
// logging level here would be a defined name when the attribute is set
if(meta.Metadata.loggingLevel == "Verbose")
{
Console.WriteLine($"verbose logging enabled for {functionName}");
}
else if(meta.Metadata.loggingLevel == "info")
{
Console.WriteLine($"info only logging enabled for {functionName}");
}
// Let the method do its own thing.
return meta.Proceed();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
return null;
}
finally
{
Console.WriteLine($"Hooked on {functionName} method end");
}
}
}[HookFunctionAndPrint]
[MetaLamaMetadata("Logging", "Verbose")]
static void Main(string[] args)
{
}propertyValueIEquatable<>Debugger.Break()dotnet build -p:MetalamaDebugCompiler=TrueIfBusinessExceptionBusinessException[BusinessException][BusinessException]
public class UserAlreadyExistsException(int age, string email) : Exception;[Introduce]BuildAspectstring.Format("The user with age {0} and email {1} already exists", age, email);public class MyType
{
private readonly ILogger<MyType> _logger;
public MyType(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<MyType>();
}
}var expr = new ExpressionBuilder();
expr.AppendVerbatim("var propertyValue = (");
expr.AppendType(field.Type);
expr.AppendVerbatim(")valueType?.GetValue(");
//And so onif (typeof(IEnumerable).IsAssignableFrom(prop.Type.ToType()))
{
var genericTypes = prop.Type.ToType().GetInterfaces().FirstOrDefault(t =>
t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>));
if (genericTypes == null)
continue;
var innerGenericType = genericTypes.GetGenericArguments()[0];