Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hello All,

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

C#
[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
Comments
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?
--SA

Your constructor is declared wrong. It should look like:

C#
public MyAttributesAttribute(string[] test)
{
    //...
}


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

C#
[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)[^]
 
Share this answer
 
Comments
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.
--SA
markovl 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.

Cheers,
--SA
What you are trying to do is not allowed in .NET. For C#, here is the list of allowed attribute parameter types:
http://msdn.microsoft.com/en-us/library/aa664615%28v=vs.71%29.aspx[^].

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.

—SA
 
Share this answer
 
v2
Comments
VJ Reddy 20-Jun-12 13:15pm    
Good answer. 5!
Sergey Alexandrovich Kryukov 20-Jun-12 13:32pm    
Thank you, VJ.
--SA
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.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900