Great!!! The interested part was:

> public class BaseDataConverter : IJavaScriptConverter
> {
>        public BaseDataConverter() : base()
>        {
>                m_AllowInheritance = true;

Michael




On 10/5/06, dimkaspb <[EMAIL PROTECTED]> wrote:
>
> Everything appears to be working now (!!!) so I just wanted to share my
> experience in case others come across a similar issue.
>
> I have many custom objects MyDataClass1, MyDataClass2, MyDataClass3,
> etc, all of which are derived from BaseData class which adds lots of DB
> related functionality.
>
> So to be able to load my objects from DB and return them back from a
> server side Ajax method I had to do the following:
>
> 1. Create a custom converter BaseDataConverter (see code below) that
> only serializes public properties based on a certain attribute.
> 2. Update web.config jsonConverters section as follows:
>  <add type="MyNamespace.BaseDataConverter,MyAssemblyName"/>
> 3. Use my data object in Ajax method:
>
> [AjaxPro.AjaxMethod()]
> public MyDataClass1 AjaxLoadMyObject(int pk)
> {
>  MyDataClass1 myObj = null;
>  try
>  {
>    myObj = new MyDataClass1();
>    myObj.Load(pk);
>  }
>  catch (Exception ex)
>  {
>    // log error
>  }
>  return myObj;
> }
>
> It is that simple!
> Hope this helps anyone else looking at how to return your custom
> objects from ajax methods.
>
> /*** BaseDataConverter sample code **********.
>
> public class BaseDataConverter : IJavaScriptConverter
> {
>        public BaseDataConverter() : base()
>        {
>                m_AllowInheritance = true;
>
>                // list all data types that can be serialized
>                m_serializableTypes = new Type[] {
>                        typeof(MyDataClass1),
>                        typeof(MyDataClass2)
>                        // etc...
>                        // or you can only leave typeof(BaseData) and
> make sure
>                        // that m_AllowInheritance is set to true
>                        // doing that will obviously be slower though
> !!!
>                };
>        }
>
>        public override string Serialize(object o)
>        {
>                StringBuilder sb = new StringBuilder();
>                Serialize(o, sb);
>                return sb.ToString();
>        }
>
>        public override void Serialize(object o, StringBuilder sb)
>        {
>                bool b = true;
>                Type type = o.GetType();
>
>                sb.Append('{');
>                sb.Append("\"__type\":");
>                JavaScriptUtil.QuoteString(type.AssemblyQualifiedName, sb);
>                b = false;
>
>                // find members
>                MemberFilter memberFilter = new
> MemberFilter(BaseDataConverter.FilterFindMembers);
>                MemberInfo[] members = type.FindMembers(MemberTypes.Property,
> BindingFlags.Instance | BindingFlags.Public, memberFilter, null);
>                string[] mappedMembers = new string[members.Length];
>                for (int i = 0; i < mappedMembers.Length; i++)
>                {
>                        mappedMembers[i] = members[i].Name;
>                }
>
>                // get values
>                foreach (string mappedMember in mappedMembers)
>                {
>                        if(!b) sb.Append(",");
>                        JavaScriptUtil.QuoteString(mappedMember, sb);
>                        sb.Append(':');
>                        
> JavaScriptSerializer.Serialize(type.GetProperty(mappedMember).GetValue(o,
> null), sb);
>                }
>
>                sb.Append('}');
>        }
>
>        private static bool FilterFindMembers(MemberInfo mi, Object search)
>        {
>                object[] attribs =
> mi.GetCustomAttributes(typeof(ControlTypeAttribute), true);
>
>                if (attribs != null)
>                        if (attribs.Length > 0)
>                                return true;
>
>                return false;
>        }
>
> }
>
> -- Dima.
>
>
> >
>


-- 
Best regards | Schöne Grüße
Michael

Microsoft MVP - Most Valuable Professional
Microsoft MCAD - Certified Application Developer

http://weblogs.asp.net/mschwarz/
http://www.schwarz-interactive.de/

Skype: callto:schwarz-interactive
MSN IM: [EMAIL PROTECTED]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Ajax.NET Professional" group.

To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]

For more options, visit this group at http://groups.google.com/group/ajaxpro

The latest downloads of Ajax.NET Professional can be found at 
http://www.ajaxpro.info/

Don't forget to read my blog at http://weblogs.asp.net/mschwarz/

The open source project is now located at
http://www.codeplex.com/Wiki/View.aspx?ProjectName=AjaxPro
-~----------~----~----~----~------~----~------~--~---

Reply via email to