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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Persistant DefaultValueAttributes

Persistant DefaultValueAttributes

Scheduled Pinned Locked Moved C#
jsonquestion
2 Posts 2 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.
  • A Offline
    A Offline
    ajenny
    wrote on last edited by
    #1

    Hi, Let's have a dream. I have a class with some properties, here a sample : public class TestClass { private int myint; private string mystring = ""; public TestClass() { } [DefaultValue(100)] public int IntProp { get { return myint; } set { myint = value; } } [DefaultValue("idea")] public string StringProp { get { return mystring ; } set { mystring = value; } } } I wonder if it is possible to set at runtime the DefaultValue for every properties. My wish is to be able to serialize theses values. So at next startup, the new defaultvalues will be used. So what are the issues ? - It's straightforward to enumerate the current DefaultValueAttribute of every properties. - I think I have to use the ICustomTypeDescriptor to get a dynamic defaultValue attribute. - I really don't know where to put the serialization and deserialization code for putting and getting back the default values. Constructor ? Desctructor ? - Another point, by setting the DefaultValueAttribute of one propertie, will it be set for every instance of the class ? Thanks for helping me. --- Alexandre Jenny

    H 1 Reply Last reply
    0
    • A ajenny

      Hi, Let's have a dream. I have a class with some properties, here a sample : public class TestClass { private int myint; private string mystring = ""; public TestClass() { } [DefaultValue(100)] public int IntProp { get { return myint; } set { myint = value; } } [DefaultValue("idea")] public string StringProp { get { return mystring ; } set { mystring = value; } } } I wonder if it is possible to set at runtime the DefaultValue for every properties. My wish is to be able to serialize theses values. So at next startup, the new defaultvalues will be used. So what are the issues ? - It's straightforward to enumerate the current DefaultValueAttribute of every properties. - I think I have to use the ICustomTypeDescriptor to get a dynamic defaultValue attribute. - I really don't know where to put the serialization and deserialization code for putting and getting back the default values. Constructor ? Desctructor ? - Another point, by setting the DefaultValueAttribute of one propertie, will it be set for every instance of the class ? Thanks for helping me. --- Alexandre Jenny

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Implement the ICustomTypeDescriptor in your class. In the implementation for GetProperties you can attach all the attributes to your PropertyDescriptors that you want. I use this for an object with "properties" that are allocated from a database, and I add a DefaultValueAttribute to those PropertyDescriptors from the database as well. If you simply want to serialize these, then you use the TypeDescriptor.GetProperties method to get the PropertyDescriptorCollection and enumerate those properties, getting the value from the DefaultValueAttribute which may be found in PropertyDescriptor.Attributes (inheritted from MemberDescriptor).

      Microsoft MVP, Visual C# My Articles

      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