By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,462 Members | 799 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,462 IT Pros & Developers. It's quick & easy.

Trying to Use System.Attribute and Reflection to return Property for use

P: n/a
I am looking to use a field attribute in a to find and use a property. If given
Find("Record Artist", "Billy Joel");
I am looking to find the first instance in the collection where the value
the property -=> Artist <=-( because of the Attribute "Record Artist")
equals "Billy Joel".

I didn't know if this is possible.

Please keep in mind that I know I need to take care of data types and
error detection/correction. I am trying to simplify this as much as possible.

Thanks,
Dave

Da**********@BellSouth.net
###########################################
public int Find(string fieldName, string fieldValue)
{
RecordAlbumCollection collection = myCollection;

object[] attribs = j.GetType().GetCustomAttributes(typeof(FieldNameTe xt), false);
FieldNameText field = attribs[0] as FieldNameText;

// Find the Property Name
foreach (FieldNameText f in field)
{
if (fieldName.Equals(f.FieldName))
{
// save SOMEPROPERTY
break;
}
}

foreach (RecordAlbum album in collection)
{
if (album.SOMEPROPERTY.Equals(fieldValue) == 0
return (album);
}

return null;
}

###########################################

public class RecordAlbum
{
private string m_artist;
private string m_albumtitle;
private string m_song;

[FieldNameText( "Record Artist")]
public string Artist {get {return m_artist;} set {m_artist = value;}}

[FieldNameText( "Record Title")]
public string Artist {get {return m_albumtitle;} set {m_albumtitle= value;}}

[FieldNameText( "Record Song")]
public string Artist {get {return m_song;} set {m_song= value;}}

}

###########################################

[System.AttributeUsage( AttributeTargets.Field )]
public class FieldNameText : System.Attribute
{
// Data Members
private string m_fieldName;

// Properties
public string FieldName { get { return m_fieldName; }}

public FieldNameText( string fieldName )
{
m_fieldName = fieldName;
}
}
Jul 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.