in

@Prog! - ASP.NET(C#) AJAX -

ASP.NET(C#) 2.0 & ASP.NET Ajax (ATLAS) のメモ書き

カスタム属性を作る

最新の投稿は、投稿日時: 2008/01/10 18:04 投稿者: ASANO です。スレッドには 2 件の返答があります。
ページ 1 / 1 (3 アイテム)
投稿の並べ替え: 前へ 次へ
  • 2008/01/10 18:02

    カスタム属性を作る

    [AttributeUsage(AttributeTargets.Property)]
    public class MappingAttribute : System.Attribute
    {
        private string _name;
        public MappingAttribute(string name)
        {
            _name = name;
        }
        public string Name2
        {
            get { return _name; }
        }
    }

  • 2008/01/10 18:03 回答元:

    Re: カスタム属性を作る

    public class Test
    {
        public List<A> GetAttributePropertyList<T, A>(T obj, string name)
        {
            List<A> list = new List<A>();
            foreach (var property in obj.GetType().GetProperty(name).GetCustomAttributes(typeof(A), false))
            {
                list.Add((A)property);
            }
            return list;
        }
    }

  • 2008/01/10 18:04 回答元:

    Re: カスタム属性を作る

    public class MyClass1
    {
        [ColumnMapping("あいうえお")]
        public int ABC
        {
            get { return 0; }
        }
    }

    public class MyClass2
    {
        [Mapping("かきくけこ")]
        public int DEF
        {
            get { return 0; }
        }
    }

    static void Main(string[ args)
    {
        Test t = new Test();
       
        MyClass1 myClass = new MyClass1();
        foreach (var s in t.GetAttributePropertyList<MyClass1, MappingAttribute>(myClass, "ABC"))
            Console.WriteLine(s.Name);

        MyClass2 myClass2 = new MyClass2();
        foreach (var s in t.GetAttributePropertyList<MyClass2, MappingAttribute>(myClass2, "DEF"))
            Console.WriteLine(s.Name2);

        Console.Read();
    }

    タグ: ,
ページ 1 / 1 (3 アイテム)