Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c#中,断言属性应用于方法的最短方法是什么?_C#_Unit Testing_Reflection_Nunit 2.5 - Fatal编程技术网

在c#中,断言属性应用于方法的最短方法是什么?

在c#中,断言属性应用于方法的最短方法是什么?,c#,unit-testing,reflection,nunit-2.5,C#,Unit Testing,Reflection,Nunit 2.5,在c#中,断言属性应用于方法的最短方法是什么 我用的是nunit-2.5 :)我不确定nunit使用的assert方法,但您可以简单地将此布尔表达式用于传递给它的参数(假设您能够使用LINQ: methodInfo.GetCustomAttributes(attributeType, true).Any() 如果应用了该属性,则它将返回true 如果要制作通用版本(而不是使用typeof),可以使用通用方法来完成此操作: static bool IsAttributeAppliedToMeth

在c#中,断言属性应用于方法的最短方法是什么

我用的是nunit-2.5


:)

我不确定nunit使用的assert方法,但您可以简单地将此布尔表达式用于传递给它的参数(假设您能够使用LINQ:

methodInfo.GetCustomAttributes(attributeType, true).Any()
如果应用了该属性,则它将返回true

如果要制作通用版本(而不是使用typeof),可以使用通用方法来完成此操作:

static bool IsAttributeAppliedToMethodInfo<T>(this MethodInfo methodInfo) 
    where T : Attribute
{
    // If the attribute exists, then return true.
   return methodInfo.GetCustomAttributes(typeof(T), true).Any();
}
static bool是attributeapliedtomethodinfo(this MethodInfo MethodInfo)
其中T:Attribute
{
//如果该属性存在,则返回true。
return methodInfo.GetCustomAttributes(typeof(T),true).Any();
}
然后在assert方法中调用它,如下所示:

<assert method>(methodInfo.IsAttributeAppliedToMethodInfo<MyAttribute>());
(methodInfo.IsAttributeAppliedToMethodInfo());
要对表达式执行此操作,可以首先定义以下扩展方法:

public static MethodInfo 
    AssertAttributeAppliedToMethod<TExpression, TAttribute>
    (this Expression<T> expression) where TAttribute : Attribute
{
    // Get the method info in the expression of T.
    MethodInfo mi = (expression.Body as MethodCallExpression).Method;

    Assert.That(mi, Has.Attribute(typeof(TAttribute)));
}
publicstaticmethodinfo
资产属性应用方法
(此表达式)其中tatAttribute:属性
{
//获取T表达式中的方法信息。
MethodInfo mi=(expression.Body作为MethodCallExpression.Method);
Assert.That(mi,Has.Attribute(typeof(tatAttribute));
}
然后用如下代码调用它:

(() => Console.WriteLine("Hello nurse")).
    AssertAttributeAppliedToMethod<MyAttribute>();
(()=>Console.WriteLine(“你好护士”))。
AssertAttribute应用方法();

请注意,传递给该方法的参数是什么并不重要,因为该方法从未被调用,它只需要表达式。

我不确定nunit使用的assert方法,但您可以简单地将此布尔表达式用于传递给它的参数(假设您能够使用LINQ:

methodInfo.GetCustomAttributes(attributeType, true).Any()
MethodInfo mi = typeof(MyType).GetMethod("methodname");    

Assert.IsFalse (Attribute.IsDefined (mi, typeof(MyAttributeClass)));
如果应用了该属性,则它将返回true

如果要制作通用版本(而不是使用typeof),可以使用通用方法来完成此操作:

static bool IsAttributeAppliedToMethodInfo<T>(this MethodInfo methodInfo) 
    where T : Attribute
{
    // If the attribute exists, then return true.
   return methodInfo.GetCustomAttributes(typeof(T), true).Any();
}
static bool是attributeapliedtomethodinfo(this MethodInfo MethodInfo)
其中T:Attribute
{
//如果该属性存在,则返回true。
return methodInfo.GetCustomAttributes(typeof(T),true).Any();
}
然后在assert方法中调用它,如下所示:

<assert method>(methodInfo.IsAttributeAppliedToMethodInfo<MyAttribute>());
(methodInfo.IsAttributeAppliedToMethodInfo());
要对表达式执行此操作,可以首先定义以下扩展方法:

public static MethodInfo 
    AssertAttributeAppliedToMethod<TExpression, TAttribute>
    (this Expression<T> expression) where TAttribute : Attribute
{
    // Get the method info in the expression of T.
    MethodInfo mi = (expression.Body as MethodCallExpression).Method;

    Assert.That(mi, Has.Attribute(typeof(TAttribute)));
}
publicstaticmethodinfo
资产属性应用方法
(此表达式)其中tatAttribute:属性
{
//获取T表达式中的方法信息。
MethodInfo mi=(expression.Body作为MethodCallExpression.Method);
Assert.That(mi,Has.Attribute(typeof(tatAttribute));
}
然后用如下代码调用它:

(() => Console.WriteLine("Hello nurse")).
    AssertAttributeAppliedToMethod<MyAttribute>();
(()=>Console.WriteLine(“你好护士”))。
AssertAttribute应用方法();

请注意,传递给该方法的参数是什么并不重要,因为该方法从未被调用,它只需要表达式。

nunit 2.5的替代方案:

MethodInfo mi = typeof(MyType).GetMethod("methodname");    

Assert.IsFalse (Attribute.IsDefined (mi, typeof(MyAttributeClass)));
var methodInfo = typeof(MyType).GetMethod("myMethod");

Assert.That(methodInfo, Has.Attribute(typeof(MyAttribute)));

nunit 2.5的替代方案:

var methodInfo = typeof(MyType).GetMethod("myMethod");

Assert.That(methodInfo, Has.Attribute(typeof(MyAttribute)));

调用是Assert.IsTrue-btw,他可能不知道如何获取MethodInfo。我可以使用字符串获取方法info np。希望使用表达式方法。认为有人可能已经有了一些用于此类型测试的代码。请忽略该请求。字符串可以解决此问题。调用是Assert.IsTrue-btw,他可能不知道如何获取掌握MethodInfo我可以用字符串获取方法info np。我希望有一种表达式方法。我想可能有人已经有了一些用于此类型测试的代码。请删除该请求。字符串可以解决此问题。