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.aspxHowever 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..
- using Csla.Core;
- using Csla.Core.FieldManager;
- namespace BO
- {
-
- [Serializable()]
- public class MyPropertyInfo<T> : PropertyInfo<T>
- {
-
- private T _origValue;
- private T _defaultValue;
-
- #region " Constructors "
-
- public MyPropertyInfo(string name) : base(name)
- {
- }
-
- public MyPropertyInfo(string name, string friendlyName) : base(name, friendlyName)
- {
- _defaultValue = GetDefaultValue();
- }
-
- public MyPropertyInfo(string name, string friendlyName, T defaultValue) : base(name, friendlyName, defaultValue)
- {
- _defaultValue = defaultValue;
- }
-
- private T GetDefaultValue()
- {
- T result = default(T);
- if (typeof(T).Equals(typeof(string))) {
- result = (T)(object)string.Empty;
- }
- 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))) {
-
- result = (T)(object)0;
- }
- else {
- result = null;
- }
- return result;
- }
-
- #endregion
-
- public T OrigValue {
- get { return _origValue; }
- set { _origValue = value; }
- }
-
- public override T DefaultValue {
- get { return _defaultValue; }
- }
- }
- }
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