Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.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#_.net_Reflection_Attributes - Fatal编程技术网

C# 从属性构造函数内部获取应用于哪个属性的成员?

C# 从属性构造函数内部获取应用于哪个属性的成员?,c#,.net,reflection,attributes,C#,.net,Reflection,Attributes,我有一个自定义属性,在自定义属性的构造函数中,我想将属性的值设置为应用该属性的属性的类型,是否有某种方法可以从属性类内部访问应用该属性的成员?恐怕属性不是这样工作的。它们仅仅是“标记”,附着在物体上,但无法与之交互 属性本身通常应该没有行为,只包含它们所附加到的类型的元数据。与属性关联的任何行为都应该由另一个类提供,该类查找属性的存在并执行任务 如果您对应用属性的类型感兴趣,则在您反射以获取属性的同时,该信息将可用。您可以执行下一步操作。这是一个简单的例子 //target class publ

我有一个自定义属性,在自定义属性的构造函数中,我想将属性的值设置为应用该属性的属性的类型,是否有某种方法可以从属性类内部访问应用该属性的成员?

恐怕属性不是这样工作的。它们仅仅是“标记”,附着在物体上,但无法与之交互

属性本身通常应该没有行为,只包含它们所附加到的类型的元数据。与属性关联的任何行为都应该由另一个类提供,该类查找属性的存在并执行任务


如果您对应用属性的类型感兴趣,则在您反射以获取属性的同时,该信息将可用。

您可以执行下一步操作。这是一个简单的例子

//target class
public class SomeClass{

    [CustomRequired(ErrorMessage = "{0} is required", ProperytName = "DisplayName")]
    public string Link { get; set; }

    public string DisplayName { get; set; }
}
    //custom attribute
    public class CustomRequiredAttribute : RequiredAttribute, IClientValidatable
{
    public string ProperytName { get; set; }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var propertyValue = "Value";
        var parentMetaData = ModelMetadataProviders.Current
             .GetMetadataForProperties(context.Controller.ViewData.Model, context.Controller.ViewData.Model.GetType());
        var property = parentMetaData.FirstOrDefault(p => p.PropertyName == ProperytName);
        if (property != null)
            propertyValue = property.Model.ToString();

        yield return new ModelClientValidationRule
        {
            ErrorMessage = string.Format(ErrorMessage, propertyValue),
            ValidationType = "required"
        };
    }
}
//目标类
公共类{
[CustomRequired(ErrorMessage=“{0}是必需的”,ProperytName=“DisplayName”)]
公共字符串链接{get;set;}
公共字符串DisplayName{get;set;}
}
//自定义属性
公共类CustomRequiredAttribute:RequiredAttribute,IClientValidable
{
公共字符串propertname{get;set;}
公共IEnumerable GetClientValidationRules(ModelMetadata元数据、ControllerContext上下文)
{
var propertyValue=“Value”;
var parentMetaData=modelmataproviders.Current
.GetMetadataForProperties(context.Controller.ViewData.Model、context.Controller.ViewData.Model.GetType());
var property=parentMetaData.FirstOrDefault(p=>p.PropertyName==PropertyName);
if(属性!=null)
propertyValue=property.Model.ToString();
返回新的ModelClientValidationRule
{
ErrorMessage=string.Format(ErrorMessage,propertyValue),
ValidationType=“必需”
};
}
}

可以从.NET 4.5使用
CallerMemberName

[SomethingCustom]
public string MyProperty { get; set; }
那么您的属性:

[AttributeUsage(AttributeTargets.Property)]
public class SomethingCustomAttribute : Attribute
{
    public StartupArgumentAttribute([CallerMemberName] string propName = null)
    {
        // propName == "MyProperty"
    }
}

你能简要描述一下用例吗?如果你能提供你正在解决的问题的更多细节,也许可以提供一个替代解决方案。谢谢,我知道如何以不同的方式实现同样的目标,但我想知道这是否可行,因为代码会更干净。正如你所说,通过反射获取自定义属性时,您知道的类型并不是世界末日,但如果传递给GetCustomAttribute的类型也存储在系统中,则“很好”。AttributeThat现在应该是标记的答案!正是我需要的,谢谢!这是不久前添加的,但我有一个后续问题:根据此链接,[[CallerMemberName]只提供成员名称,而不是完全限定的方式。因此,如果我想通过反射获取该类型,我如何知道它的全名?