Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to Get Property attribute from instance variable

How to Get Property attribute from instance variable

Scheduled Pinned Locked Moved C#
csharphelptutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mmdullah
    wrote on last edited by
    #1

    public class MyClass { private string _MyProperty = "the property"; [colName="theColumn"] public string MyProperty { get { return _MyProperty; } set { _MyProperty = value; } } public string GetName() { // code to implement } } public class MyClassTest { MyClass c=new MyClass(); // I want to get the attribute in the following way: string colName= c.MyProperty.GetName(); //colName should be theColumn } any kind of help would be appriciated. and it should be in .NET 2.0 framework

    C 1 Reply Last reply
    0
    • M mmdullah

      public class MyClass { private string _MyProperty = "the property"; [colName="theColumn"] public string MyProperty { get { return _MyProperty; } set { _MyProperty = value; } } public string GetName() { // code to implement } } public class MyClassTest { MyClass c=new MyClass(); // I want to get the attribute in the following way: string colName= c.MyProperty.GetName(); //colName should be theColumn } any kind of help would be appriciated. and it should be in .NET 2.0 framework

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Why do you want to do this ? The value is going to exist at the class level, you can't change attributes for class instances AFAIK. They are used to specify things for all instances of a class.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      M 1 Reply Last reply
      0
      • C Christian Graus

        Why do you want to do this ? The value is going to exist at the class level, you can't change attributes for class instances AFAIK. They are used to specify things for all instances of a class.

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        M Offline
        M Offline
        mmdullah
        wrote on last edited by
        #3

        I want to do this for the following scenarios: say I want to Get an object into database. And I need the column name for WHERE clause e.g // Get employees Get method is like as follows EmpDA.Get(string filterExpression) { //------- the code } Employee emp=DAEmp.GetByID(7); emp.Salary=20000; EmpDA.Get(string.format("{0}>{1}",emp.Salary.GetFieldName,emp.Salary); please look at the following factors. 1) I am getting emp list using where clause as "Salary> 20000" 2) But I want to Get the column name "Salary" using emp.Salary.GetFieldName() thanks for your reply

        K 1 Reply Last reply
        0
        • M mmdullah

          I want to do this for the following scenarios: say I want to Get an object into database. And I need the column name for WHERE clause e.g // Get employees Get method is like as follows EmpDA.Get(string filterExpression) { //------- the code } Employee emp=DAEmp.GetByID(7); emp.Salary=20000; EmpDA.Get(string.format("{0}>{1}",emp.Salary.GetFieldName,emp.Salary); please look at the following factors. 1) I am getting emp list using where clause as "Salary> 20000" 2) But I want to Get the column name "Salary" using emp.Salary.GetFieldName() thanks for your reply

          K Offline
          K Offline
          King Julien
          wrote on last edited by
          #4

          As Graus said, i too dont find its a better solution for this problem! but still, you can get the custom attribute values by the following statement.

          colNameAttribute mAttribInstance = (colNameAttribute)c.GetType()
          .GetCustomAttributes(typeof(colNameAttribute),false).GetValue(0);

          ///remember the array index is 0. you can change it accordingly... ////

          The object mAttribInstance will hold the various properties values within your "colName" attribute class. and you can access them by their respective name.. for e.g if "name" is the string used then, mAttribInstance.Name will give you the value "theColumn"

          Have a Happy Coding.....

          M 1 Reply Last reply
          0
          • K King Julien

            As Graus said, i too dont find its a better solution for this problem! but still, you can get the custom attribute values by the following statement.

            colNameAttribute mAttribInstance = (colNameAttribute)c.GetType()
            .GetCustomAttributes(typeof(colNameAttribute),false).GetValue(0);

            ///remember the array index is 0. you can change it accordingly... ////

            The object mAttribInstance will hold the various properties values within your "colName" attribute class. and you can access them by their respective name.. for e.g if "name" is the string used then, mAttribInstance.Name will give you the value "theColumn"

            Have a Happy Coding.....

            M Offline
            M Offline
            mmdullah
            wrote on last edited by
            #5

            Thanks King Julien for your reply. I have already used it when interacting with database for insert update fields but now my problem is to get the column name "theColumn" in the following fashion. string colName= emp.Salary.GetColName(); //colName should be "theColumn"; look at the GetColName() after the property Salary. emp.Salary.GetColName(); I am using c# 2.0 framework.

            K 1 Reply Last reply
            0
            • M mmdullah

              Thanks King Julien for your reply. I have already used it when interacting with database for insert update fields but now my problem is to get the column name "theColumn" in the following fashion. string colName= emp.Salary.GetColName(); //colName should be "theColumn"; look at the GetColName() after the property Salary. emp.Salary.GetColName(); I am using c# 2.0 framework.

              K Offline
              K Offline
              King Julien
              wrote on last edited by
              #6

              i see... in that case you may need to create a new class with the function GetColName(); and use it as your return type for property. example:

              Class yourClass
              {
              public string GetColName()
              {
              colNameAttribute mAttribInstance = (colNameAttribute)this.GetType().GetCustomAttributes(typeof(colNameAttribute),false).GetValue(0);

              return mAttribInstance.Name;
              

              }
              }

              and u can set the return type of the property salary as yourclass and you will be able to access the GetColName() method!!

              Have a Happy Coding.....

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups