C# 如何访问方法的属性';s参数

C# 如何访问方法的属性';s参数,c#,reflection,attributes,C#,Reflection,Attributes,如何确定参数是否附加了自定义属性 我认为这个测试用例会通过: [TestCase("")] public void TestParameterAttribute([NotRequired]string theString) { var result = false; foreach (var attribute in theString.GetType().GetCustomAttributes(true)) {

如何确定参数是否附加了自定义属性

我认为这个测试用例会通过:

    [TestCase("")]
    public void TestParameterAttribute([NotRequired]string theString)
    {
        var result = false;

        foreach (var attribute in theString.GetType().GetCustomAttributes(true))
        {
            if (attribute.GetType() == (typeof(NotRequiredAttribute)))
            {
                result = true;
            }
        }

        Assert.That(result, Is.True);
    }

这需要更多的工作

[TestCase("")]
public void TestParameterAttribute([NotRequired]string theString)
{
    var method = MethodInfo.GetCurrentMethod();
    var parameter = method.GetParameters()[0];
    var result = false;

    foreach (var attribute in parameter.GetCustomAttributes(true))
    {
        if (attribute.GetType() == (typeof(NotRequiredAttribute)))
        {
            result = true;
        }
    }

    Assert.That(result, Is.True);
}

这需要更多的工作

[TestCase("")]
public void TestParameterAttribute([NotRequired]string theString)
{
    var method = MethodInfo.GetCurrentMethod();
    var parameter = method.GetParameters()[0];
    var result = false;

    foreach (var attribute in parameter.GetCustomAttributes(true))
    {
        if (attribute.GetType() == (typeof(NotRequiredAttribute)))
        {
            result = true;
        }
    }

    Assert.That(result, Is.True);
}

这需要更多的工作

[TestCase("")]
public void TestParameterAttribute([NotRequired]string theString)
{
    var method = MethodInfo.GetCurrentMethod();
    var parameter = method.GetParameters()[0];
    var result = false;

    foreach (var attribute in parameter.GetCustomAttributes(true))
    {
        if (attribute.GetType() == (typeof(NotRequiredAttribute)))
        {
            result = true;
        }
    }

    Assert.That(result, Is.True);
}

这需要更多的工作

[TestCase("")]
public void TestParameterAttribute([NotRequired]string theString)
{
    var method = MethodInfo.GetCurrentMethod();
    var parameter = method.GetParameters()[0];
    var result = false;

    foreach (var attribute in parameter.GetCustomAttributes(true))
    {
        if (attribute.GetType() == (typeof(NotRequiredAttribute)))
        {
            result = true;
        }
    }

    Assert.That(result, Is.True);
}
theString.GetType()
获取对表示
字符串的
类型的引用。在其上调用
GetCustomAttributes
将在
字符串中查找这些属性

你想做什么。。获取当前方法中参数的属性。也许是这样的:

var result = false;

foreach (var parameter in MethodInfo.GetCurrentMethod().GetParameters())
{
    if (parameter.GetCustomAttributes().Any(x => x.GetType() == typeof (NotRequiredAttribute)))
        result = true;
}
theString.GetType()
获取对表示
字符串的
类型的引用。在其上调用
GetCustomAttributes
将在
字符串中查找这些属性

你想做什么。。获取当前方法中参数的属性。也许是这样的:

var result = false;

foreach (var parameter in MethodInfo.GetCurrentMethod().GetParameters())
{
    if (parameter.GetCustomAttributes().Any(x => x.GetType() == typeof (NotRequiredAttribute)))
        result = true;
}
theString.GetType()
获取对表示
字符串的
类型的引用。在其上调用
GetCustomAttributes
将在
字符串中查找这些属性

你想做什么。。获取当前方法中参数的属性。也许是这样的:

var result = false;

foreach (var parameter in MethodInfo.GetCurrentMethod().GetParameters())
{
    if (parameter.GetCustomAttributes().Any(x => x.GetType() == typeof (NotRequiredAttribute)))
        result = true;
}
theString.GetType()
获取对表示
字符串的
类型的引用。在其上调用
GetCustomAttributes
将在
字符串中查找这些属性

你想做什么。。获取当前方法中参数的属性。也许是这样的:

var result = false;

foreach (var parameter in MethodInfo.GetCurrentMethod().GetParameters())
{
    if (parameter.GetCustomAttributes().Any(x => x.GetType() == typeof (NotRequiredAttribute)))
        result = true;
}

您还可以使用方法的通用版本:

parameter.GetCustomAttribute()!=无效的

您还可以使用以下方法的通用版本:

parameter.GetCustomAttribute()!=无效的

您还可以使用以下方法的通用版本:

parameter.GetCustomAttribute()!=无效的

您还可以使用以下方法的通用版本:

parameter.GetCustomAttribute()!=无效的