从特定对象的属性/字段获取自定义属性 [英] Get custom attribute from specific object property/field
问题描述
我已经搜索了一段时间,并测试了几种方法,但没有找到想要的答案。我会尝试解释。
I've been searching for a while now and tested several methods, but i didn't find the answer i was looking for. I'll try to explain.
我有一个带有多个字段/属性的对象。这些属性具有自定义属性。
我想要的是从特定属性获取自定义属性,而无需了解对象的全部知识。
I have an object with several fields/properties. These properties have custom attributes. What i want is to get the custom attribute from a specific propertie without all the knowlege of the object.
这是基类
// FieldAttr has a public Text propery
public class TestObject
{
// Declare fields
[FieldAttr("prop_testfld1")]
public FLDtype1 testfld1 = new FLDtype1();
[FieldAttr("prop_testfld2")]
public FLDtype2 testfld2 = new FLDtype2();
[FieldAttr("prop_testfld3")]
public FLDtype1 testfld3;
}
public class FLDtype1
{
public string Value { get; set; }
}
public class FLDtype2
{
public Guid Value { get; set; }
}
public sealed class FieldAttr: System.Attribute
{
private string _txt;
public EntityFieldType(string txt)
{
this._text = txt;
}
public string Text { get { return this._text; } }
}
我希望能够在我的应用程序中做到这一点:
And i want to be able to do this in my application:
static void Main(string[] args)
{
TestObject test = new TestObject();
// (Option 1: preferred)
Console.WriteLine(test.testfld1.getFieldAttr().Text);
// (Option 2)
Console.WriteLine(test.getFieldAttr(test.testfld1).Text);
}
这可能吗?我见过从对象的所有属性/字段中获取自定义属性值的方法,但没有针对特定字段的方法。
Is this possible? I've seen methods to get custom attribute values from all properties/fields of an object, but not for a specific field.
我有一个可行的方法来获取来自枚举的自定义属性,但无法为对象字段/属性重新创建它。这是因为我无法获取要尝试探索的字段的名称,因为(例如)test.testfld1.ToString()给了我 ns.FLDtype1。
I've got a working method to get custom attribute from an enum, but wasn't able to recreate it for object fields/properties. This is because i couldn't get the name of the field i was trying to explore, because (for example) test.testfld1.ToString() give's me "ns.FLDtype1".
期待答案:)
(请原谅我的英语)
Looking forward for the answer :) (and excuse my english)
推荐答案
反复试验我决定对Selman22答案进行一些修改。这是我创建的代码:
After another day of trial and error I decided to make use of Selman22 answer with a little modification. This is code I created:
public class TestObject : iTestObject
{
// Declare fields
[FieldAttr("prop_testfld1")]
public FLDtype1 testfld1 = new FLDtype1();
[FieldAttr("prop_testfld2")]
public FLDtype2 testfld2 = new FLDtype2();
[FieldAttr("prop_testfld3")]
public FLDtype1 testfld3;
}
public class FLDtype1 : iField
{
public string Value { get; set; }
}
public class FLDtype2 : iField
{
public Guid Value { get; set; }
}
public sealed class FieldAttr: System.Attribute
{
private string _txt;
public FieldAttr(string txt)
{
this._txt = txt;
}
public string Text { get { return this._txt; } }
}
public interface iField { }
public interface iTestObject { }
public static class Extensions
{
public static FieldAttr GetFieldAttr<T>(this T source, Expression<Func<iField>> field) where T : iTestObject
{
// Get member body. If no body present, return null
MemberExpression member = (MemberExpression)field.Body;
if (member == null) { return null; }
// Get field info. If no field info present, return null
FieldInfo fieldType = typeof(T).GetField(member.Member.Name);
if (fieldType == null) { return null; }
// Return custom attribute
return fieldType.GetCustomAttribute<FieldAttr>();
}
}
用法:
public class Program
{
public static void Main()
{
TestObject test = new TestObject();
Console.WriteLine(test.GetFieldAttr(() => test.testfld1).Text);
Console.WriteLine(test.GetFieldAttr(() => test.testfld2).Text);
Console.WriteLine(test.GetFieldAttr(() => test.testfld3).Text);
}
}
用途:
using System;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
我已经实现了用于保护 GetFieldAttr 方法的接口
I have implemented interfaces to protect the GetFieldAttr method
@ Sulman22:感谢Thnx!
@Sulman22: Thnx for the response!
这篇关于从特定对象的属性/字段获取自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!