PropertyInfo<T> Subclass in c# example?

PropertyInfo<T> Subclass in c# example?

Old forum URL: forums.lhotka.net/forums/t/6911.aspx


SiegfiedN posted on Thursday, May 07, 2009

Hi,

I am a total newbie to Csla and am in the process of learning and applying Csla by studying the 'Expert C# 2008 Business Objects' book.

It looks like I need to Subclass the PropertyInfo<T> class to add some custom meta data.

I am struggling to get it right however and could not find a c# example . :(

I have the following,

    [Serializable]
    class FSIPropertyInfo<T> : PropertyInfo<T> where T : new()
    {
   
    }

but I get the following error message

Error    1    'string' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'Octopus.Lib.FSIPropertyInfo<T>'   ..


for this property declaration

        //Customer Id
        private static FSIPropertyInfo<string> IDProperty = RegisterProperty(new FSIPropertyInfo<string>("Customer ID"));
        public string ID
        {
            get { return GetProperty(IDProperty); }
            set { SetProperty(IDProperty, value); }
        }


Question: Can someone post or provide a link to where I can find an example of a c# Property<T> subclass implentation?

Thank you,

Siegfried


JoeFallon1 replied on Thursday, May 07, 2009

I posted a VB version a while back. A search should locate it easily enough.

It really isn't that hard to read.

You could always use one of those translation sites to get it on C#.

I recommend:
http://www.developerfusion.com/tools/convert/vb-to-csharp/

Joe

SiegfiedN replied on Thursday, May 07, 2009

Hi Joe,

Thanks for your help Joe,.

I found the link and converted the VB.Net to c# using the link you provided. Great!
http://forums.lhotka.net/forums/post/28112.aspx

However when I build the project I now get the following compile error..

'Error    2    Cannot implicitly convert type 'Csla.PropertyInfo<string>' to 'Octopus.Lib.MyPropertyInfo<string>'. An explicit conversion exists (are you missing a cast?) ...'


This is the property declaration.

        //Customer Id
        private static MyPropertyInfo<string> IDProperty = RegisterProperty(new MyPropertyInfo<string>("Customer ID"));
        public string ID
        {
            get { return GetProperty(IDProperty); }
            set { SetProperty(IDProperty, value); }
        }

Not sure what I need to cast? Thanks.

Siegfried

SiegfiedN replied on Thursday, May 07, 2009

This compiles now..

For the reference..

This is the translated VB->c# code to subclass a PropertyInfo from Joe..
  1. using Csla.Core;
  2. using Csla.Core.FieldManager;

  3. namespace BO
  4. {
  5.    
  6.     [Serializable()]
  7.     public class MyPropertyInfo<T> : PropertyInfo<T>
  8.     {
  9.        
  10.         private T _origValue;
  11.         private T _defaultValue;
  12.        
  13.         #region " Constructors "
  14.        
  15.         public MyPropertyInfo(string name) : base(name)
  16.         {
  17.         }
  18.        
  19.         public MyPropertyInfo(string name, string friendlyName) : base(name, friendlyName)
  20.         {
  21.             _defaultValue = GetDefaultValue();
  22.         }
  23.        
  24.         public MyPropertyInfo(string name, string friendlyName, T defaultValue) : base(name, friendlyName, defaultValue)
  25.         {
  26.             _defaultValue = defaultValue;
  27.         }
  28.        
  29.         private T GetDefaultValue()
  30.         {
  31.             T result = default(T);
  32.             if (typeof(T).Equals(typeof(string))) {
  33.                 result = (T)(object)string.Empty;
  34.             }
  35.             else if (typeof(T).Equals(typeof(int)) || typeof(T).Equals(typeof(long)) || typeof(T).Equals(typeof(decimal)) || typeof(T).Equals(typeof(short)) || typeof(T).Equals(typeof(byte))) {
  36.                
  37.                 result = (T)(object)0;
  38.             }
  39.             else {
  40.                 result = null;
  41.             }
  42.             return result;
  43.         }
  44.        
  45.         #endregion
  46.        
  47.         public T OrigValue {
  48.             get { return _origValue; }
  49.             set { _origValue = value; }
  50.         }
  51.        
  52.         public override T DefaultValue {
  53.             get { return _defaultValue; }
  54.         }
  55.     }
  56. }

you then also need to cast your property declaration as follows..


        //Customer Id
        private static MyPropertyInfo<string> IDProperty = RegisterProperty(new MyPropertyInfo<string>("Customer ID"))  as MyPropertyInfo<string>;
        public string ID
        {
            get { return GetProperty(IDProperty); }
            set { SetProperty(IDProperty, value); }
        }

Siegfried

SiegfiedN replied on Thursday, May 07, 2009


result = (T)(object)null;

rather in the

private T GetDefaultValue()

method if required...

Copyright (c) Marimer LLC