title: C# Accessing custom attribute of owner object
body: <blockquote>
    <p>How can I access the custom attribute of the parent or owner object.
    Look at the FieldInfo property of the SQLFieldInfo struct</p>
    
    <p>Here's a more detailed program that will compile and run that shows what I need.</p>
  </blockquote>
  
  <pre><code>public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
  
          Employee myclass = new Employee();
  
          // Load from sql server...
          myclass.Name = "Alain";
          myclass.Age = 51;
          //----
  
          MessageBox.Show(myclass.Name.ToString()); // Should return Alain
          MessageBox.Show(myclass.Age.FieldInfo.Type.ToString()); // Should output "int"
      }
  }
  
  // This next class is generated by a helper exe that reads SQL table design and create the class from it
  [SQLTableAttribute(DatabaseName = "Employees", Schema = "dbo", TableName = "Employees")]
  public class Employee
  {
      [SQLFieldAttribute(FieldName = "ID", Type = SqlDbType.Int)]
      public SQLFieldInfo&lt;int&gt; ID { get; set; }
  
      [SQLFieldAttribute(FieldName = "Name", Type = SqlDbType.NVarChar, Size = 200)]
      public SQLFieldInfo&lt;String&gt; Name { get; set; }
  
      [SQLFieldAttribute(FieldName = "Age", Type = SqlDbType.Int)]
      public SQLFieldInfo&lt;int&gt; Age { get; set; }
  }
  
  
  public struct SQLFieldInfo&lt;T&gt;
  {
  
      private readonly T value;
  
      public SQLFieldInfo(T Value) 
      {
          this.value = Value;
      }
  
  
      public static implicit operator SQLFieldInfo&lt;T&gt;(T Value)
      {
          return new SQLFieldInfo&lt;T&gt;(Value);
      }
  
  
      public T Value
      {
          get
          {
              return this.value;
          }
      }
  
  
      public override string ToString()
      {
          return this.value.ToString();
      }
  
  
      public SQLFieldAttribute FieldInfo
      {
          get
          {
              // Need to retreive the attribute class of the parent or declaring member
  
              return null;
          }
      }
  }
  
  // Holds the sql field information
  public class SQLFieldAttribute : Attribute
  {
      public string FieldName { get; set; }
      public SqlDbType Type { get; set; }
      public bool AllowNull { get; set; }
      public int Size { get; set; }
  }
  
  // Holds the sql table information
  public class SQLTableAttribute : Attribute
  {
      public string DatabaseName { get; set; }
      public string Schema { get; set; } = "dbo";
      public string TableName { get; set; }
  }
  </code></pre>
  
  <p>Thank you!</p>
  
  <p>Alain</p>