C# 统一拦截概念清晰

C# 统一拦截概念清晰,c#,unity-container,unity-interception,C#,Unity Container,Unity Interception,我正在跟踪链接,以便在我的项目中实现统一 通过以下链接,我创建了一个类,如下所示: [AttributeUsage(AttributeTargets.Method)] public class MyInterceptionAttribute : Attribute { } public class MyLoggingCallHandler : ICallHandler { IMethodReturn ICallHandler.Invoke(IMethodInvocation inpu

我正在跟踪链接,以便在我的项目中实现统一

通过以下链接,我创建了一个类,如下所示:

[AttributeUsage(AttributeTargets.Method)]
public class MyInterceptionAttribute : Attribute
{

}

public class MyLoggingCallHandler : ICallHandler
{
    IMethodReturn ICallHandler.Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        IMethodReturn result = getNext()(input, getNext);
        return result;
    }
    int ICallHandler.Order { get; set; }
}

public class AssemblyQualifiedTypeNameConverter : ConfigurationConverterBase
{
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (value != null)
        {
            Type typeValue = value as Type;
            if (typeValue == null)
            {
                throw new ArgumentException("Cannot convert type", typeof(Type).Name);
            }
            if (typeValue != null) return (typeValue).AssemblyQualifiedName;
        }
        return null;
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        string stringValue = (string)value;
        if (!string.IsNullOrEmpty(stringValue))
        {
            Type result = Type.GetType(stringValue, false);
            if (result == null)
            {
                throw new ArgumentException("Invalid type", "value");
            }
            return result;
        }
        return null;
    }
}
到目前为止,我没有做什么特别的事情,只是按照上面链接中解释的例子。但是,当我必须实现Unity拦截类时,我遇到了很多困惑

假设我必须在类中的一个方法上实现,如:

[MyInterception]
public Model GetModelByID(Int32 ModelID)
{
    return _business.GetModelByID(ModelID);
}
这是我遇到的主要问题,我不知道如何通过GetModelByID()方法使用Intercept类,以及如何获得统一性


请帮助我,并请解释Unity拦截的概念。

解释Unity拦截

拦截是一个可以将“核心”代码与其他关注点隔离开来的概念。在您的方法中:

public Model GetModelByID(Int32 ModelID)
{
   return _business.GetModelByID(ModelID);
}
您不想用其他代码“污染”它,比如日志记录、评测、缓存等,这不是该方法的核心概念,unity拦截将帮助您做到这一点

通过拦截,您可以向现有代码添加功能,而无需接触实际代码

您的具体问题

如果我的_业务为null,则不应调用GetModelById(),我如何实现这一点

通过使用截取和反射,您实际上可以实现您想要做的事情。我现在没有访问开发环境的权限,但是像这样的东西应该可以工作

IMethodReturn ICallHandler.Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
    IMethodReturn result = input.CreateMethodReturn(null, new object[0]);

    var fieldInfos = input.Target.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
    var businessField = fieldInfos.FirstOrDefault(f => f.Name == "_business");

    if (businessField != null && businessField.GetValue(input.Target) != null)
        result = getNext()(input, getNext);

    return result;
}

您可以访问方法(您正在拦截的方法)所属的目标(对象),然后通过反射读取该对象私有字段的值。

您希望拦截什么或添加什么功能。基本上,
ICallHandler
实现什么都不做。它调用您的GetModelById方法并返回值。@Jehof如果我只想这样做,那么如果我的业务为null,那么就不应该调用GetModelById(),我该如何实现呢?如果
\u business
为null,则在
GetModelById
方法中抛出异常。拦截不是支持这一点的合适工具。@Jehof这是我不能做的,我被要求执行一项任务,我必须通过拦截来完成任务。我知道
If
非常简单,但我必须使用拦截。如果
\u business
为空,您希望返回什么?如果设置了业务,您的ICallHandler必须检查被拦截的对象。基本上,如果我必须定义Unity Interception,我可以如何定义,我在google上查看了一下,但没有找到任何特定和限定的答案,请您解释一下。您是否在寻找Unity Interception的定义/摘要?不确定我是否理解你的问题这就是我要找的forMan,你做得很好,还有一个问题,你定义的方法
ICallHandler.Invoke
,我如何调用这个函数,或者这个函数是在我的程序运行时自动执行的。你不需要自己调用这个函数,Unity将自动为您解决此问题。祝您的实施顺利!