Click here to Skip to main content
Rate this: bad
good
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
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
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)[^]
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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
Lyuben Markov at 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 at 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
Rate this: bad
good
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:
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
  Permalink  
v2
Comments
VJ Reddy at 20-Jun-12 13:15pm
   
Good answer. 5!
Sergey Alexandrovich Kryukov at 20-Jun-12 13:32pm
   
Thank you, VJ.
--SA
SoMad at 20-Jun-12 15:53pm
   
Good answer. I hope OP comes back with clarification.
 
Soren Madsen
Sergey Alexandrovich Kryukov at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 535
1 CPallini 450
2 OriginalGriff 270
3 George Jonsson 256
4 Suvabrata Roy 229
0 OriginalGriff 4,873
1 CPallini 3,900
2 Sergey Alexandrovich Kryukov 3,474
3 George Jonsson 2,646
4 Gihan Liyanage 2,236


Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 20 Jun 2012
Copyright © CodeProject, 1999-2014
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