0

I am using file helpers and I put on top of my class [DelimitedRecord("|")]

I want to check though if the value is a "|" if not then I want to throw an exception..

public void WriteResults<T>(IList<T> resultsToWrite, string path, string fileName) where T: class
        {      
            var attr =  (DelimitedRecordAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(DelimitedRecordAttribute));

            if (attr.HasValue("|")) // has value does not exist.
            {
                FileHelperEngine<T> engine = new FileHelperEngine<T>();
                engine.HeaderText = String.Join("|", typeof(T).GetFields().Select(x => x.Name));

                string fullPath = String.Format(@"{0}\{1}-{2}.csv", path, fileName, DateTime.Now.ToString("yyyy-MM-dd"));

                engine.WriteFile(fullPath, resultsToWrite);
            }


        }

What can I use to check for that attribute is on the class with that value?

Edit

This is what I see as available properties

available properties

chobo2
  • 83,322
  • 195
  • 530
  • 832

1 Answers1

3

You can retrieve an instance of DelimitedRecordAttribute like this:

// t is an instance of the class decorated with your DelimitedRecordAttribute
DelimitedRecordAttribute myAttribute =
        (DelimitedRecordAttribute) 
        Attribute.GetCustomAttribute(t, typeof (DelimitedRecordAttribute));

If DelimitedRecordAttribute exposes a means of getting at the parameter (which it should), you can access the value via that means (typically a property), e.g. something like:

var delimiter = myAttribute.Delimiter

http://msdn.microsoft.com/en-us/library/71s1zwct.aspx

UPDATE

Since there doesn't seem to be a public property in your case, you can use reflection to enumerate the non-public fields and see if you can locate a field that holds the value, e.g. something like

FieldInfo[] fields = myType.GetFields(
                     BindingFlags.NonPublic | 
                     BindingFlags.Instance);

foreach (FieldInfo fi in fields)
{
    // Check if fi.GetValue() returns the value you are looking for
}

UPDATE 2

If this attribute is from filehelpers.sourceforge.net, the field you are after is

internal string Separator;

The full source code for that class is

[AttributeUsage(AttributeTargets.Class)]
public sealed class DelimitedRecordAttribute : TypedRecordAttribute
{
    internal string Separator;

/// <summary>Indicates that this class represents a delimited record. </summary>
    /// <param name="delimiter">The separator string used to split the fields of the record.</param>
    public DelimitedRecordAttribute(string delimiter)
    {
        if (Separator != String.Empty)
            this.Separator = delimiter;
        else
            throw new ArgumentException("sep debe ser <> \"\"");
    }


}

UPDATE 3

Get the Separator field like this:

FieldInfo sepField = myTypeA.GetField("Separator", 
                        BindingFlags.NonPublic | BindingFlags.Instance);

string separator = (string)sepField.GetValue();
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Ya I was looking at the msdn example. I don't see any property that I could use expect for something called TypeId what is an object. I don't see anything in it though that stores the pipe. – chobo2 Dec 12 '12 at 22:45
  • @chobo2: If there is no property that exposes the value, you can probably still get it via reflection (you can certainly get it via reflection unless the value is never stored in the instance). – Eric J. Dec 12 '12 at 22:46
  • @chobo2: Updated my post to show you how to enumerate the non-public fields (most likely place the value is stored if there's no public property). You'll need to examine the fields and see if one holds the value you are after. – Eric J. Dec 12 '12 at 22:48
  • @chobo2: Also if this is from http://filehelpers.sourceforge.net, that is open source so you could add a property that exposes the value. – Eric J. Dec 12 '12 at 22:52
  • Are you sure it is GetFields? Since when I do that I just get all the properties in that class that the attribute is on. But I don't see the attribute. – chobo2 Dec 12 '12 at 22:52
  • I am not sure what you mean. The attribute is from file helpers library. – chobo2 Dec 12 '12 at 23:02
  • Yes I am using that library. I don't see "Seperator" thought. How do I get at it? – chobo2 Dec 12 '12 at 23:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21040/discussion-between-eric-j-and-chobo2) – Eric J. Dec 12 '12 at 23:15