Click here to Skip to main content
12,942,930 members (57,010 online)
Rate this:
Please Sign up or sign in to vote.
Hello All,

I made a custom attribute but i cant able to use as i needed, my Requirement is

[MyAttributes(Language1[0] = "some text")] // not working
        public void method1()
// Attribute class is
 [AttributeUsage(AttributeTargets.All,AllowMultiple =   true)]
 public class MyAttributesAttribute: Attribute
    public AttributeUsage(string[] test)
            this.Language1 = test;
        private string[] _language1 = new string[3];
        public string[] Language1
            get { return _language1; }
            set { _language1 = value; }

Can you help me to solve this problem , i like to call Attribute call by using Index [MyAttributes(Language1[0]="some text" is it possible ............
Posted 20-Jun-12 6:27am
Sergey Alexandrovich Kryukov 20-Jun-12 12:41pm
Why? If you are trying to have something unusual (in this case, this is something not allowed by .NET), you need to explain why, if you want some constructive advice. What is your final goal?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Your constructor is declared wrong. It should look like:

public MyAttributesAttribute(string[] test)

Then when you use it you actually call it's constructor:

[MyAttributes(new string[] {"0", "1", "2"})]
public void SomeMethod()

Also I'd suggest you to read on the subject.
There's a nice article here on CP: Attributes in C#[^];
Also MSDN can be a good reference: Attributes (C# and Visual Basic)[^]
Sergey Alexandrovich Kryukov 20-Jun-12 14:42pm
This is a valid option, good suggestion; I voted 5.
Anyway, it would be better to hear from OP what is the goal of using the attribute, otherwise your code may or may not be useful.
Lyuben Markov 20-Jun-12 15:14pm
Hey, thanks! I agree completely.
Though, my observation is that people rarely care to clarify their questions, so I guess we'll never know the intent and is this approach at all valid :) I just hope that he/she (or any other ending up here with similar problems) at least check out the links and try to get the concept before using it.
Sergey Alexandrovich Kryukov 20-Jun-12 15:40pm
It's useful to leave at least some information which would provide a hint (that's why I voted this way), but in many cases, authors of the questions do not really understand what they want. In a dialog, they would be able to get some help, but at the forum it can be pretty much useless. We throw some information (after all, it could be useful for other readers) and leave it.

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

What you are trying to do is not allowed in .NET. For C#, here is the list of allowed attribute parameter types:[^].

Your declaration is also not allowed by syntax, because a parameter itself, when named, should be a property name, and you are trying to write something which takes the form of an array element (it's not even clear what meaning did you imply by that).

Please see my comment to the question. I explained what you cannot do and why. If you need some solution of a problem, you need to explain what is the problem, in terms of you final goal. What effect do you want to achieve? I hope I would know some work-around.

VJ Reddy 20-Jun-12 13:15pm
Good answer. 5!
Sergey Alexandrovich Kryukov 20-Jun-12 13:32pm
Thank you, VJ.
SoMad 20-Jun-12 15:53pm
Good answer. I hope OP comes back with clarification.

Soren Madsen
Sergey Alexandrovich Kryukov 20-Jun-12 16:46pm
Thank you. Very often, there is some work-around, but it totally depends on the goal.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,633
CHill60 2,880
Maciej Los 2,268
Jochen Arndt 1,900
ppolymorphe 1,755

Advertise | Privacy | Mobile
Web01 | 2.8.170518.1 | Last Updated 20 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100