Adding Attributes (or equivalent effect) Without Touching Original Source Code

Tech-Archive recommends: Fix windows errors by optimizing your registry



I'm pretty sure the answer to my question here is "of course no... can't
believe you'd even ask!" - but I'll ask anyway in case some of you might
have ideas for how I can achieve the objective.

So here's the question:
Is it possible to somehow add attributes to existing classes (or runtime
objects) without touching the source code of the original class definition?

What I'm doing is "ajaxifying" an existing ASP.NET application that has been
around for several years. I plan to add several Web services that return
JSON representations of server objects (e.g., Person, as in below sample
code).

An important server-side task is to serialize server-side objects to JSON,
returning the JSON string. I will be using the
System.Runtime.Serialization.Json.DataContractJsonSerializer class to
perform the serialization. That serializer class works best for this purpose
when the class to be serialized is annotated with a DataContract, including
the DataMember attribute on members to be included in the serialized object
graph. The "Person" class definition below shows what a properly attributed
class definition looks like.

[DataContract(Name = "Person", Namespace = "")]
public class Person
{
public Person(string firstName, string middleName, string lastName)
{
this.FirstName = firstName;
this.MiddleName = middleName;
this.LastName = lastName;
}
[DataMember(Name = "FirstName", Order = 1)]
public string FirstName { get; set; }

[DataMember(Name = "MiddleName", Order = 2)]
public string MiddleName { get; set; }

[DataMember(Name = "LastName", Order = 3)]
public string LastName { get; set; }
}

I do in fact have the source code of the classes that have been part of this
system for many years. So I can (and likely will) go through and add the
attributes as necessary. But it would be great if I didn't have to touch the
original source code for these reasons : 1- The guy who wrote those original
classes is Luddite who won't want all those attributes polluting his source
code; and (2) I was wondering for "academic curiosity" reasons. Ultimately I
believe I'll be adding these attributes to the code, but wanted to get some
additional perspective on this, if any is to be had.

Thanks!


.



Relevant Pages

  • Re: Unable to Serialize
    ... I needed to make the RemoteUserDataBean Serializable (as well as the ... public class RemoteUserServiceService: ... public string employeeId; ... you posted nothing to serialize. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Can I store a C# Class Instance to the Server Cache?
    ... Server Cache. ... SessionCache class instance involve a thread context switch from 987 to 345? ... public string SID; ... anything explicitly to serialize this into the cache? ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Function Serialization
    ... shelve uses pickle to serialize objects. ... name of its defining module, and upon loading attempts to find that ... You can use the inspect module to get function source code, ...
    (comp.lang.python)
  • Re: XmlSerializer would not serialize properties in the class derived from List
    ... custome property in a custom collection class by "design". ... public string ID ... when I use XmlSerializer to serialize the object. ...
    (microsoft.public.dotnet.xml)
  • Re: Serializing Array without Array
    ... > public string imagecontext; ... >> I am trying to serialize a class with an array in it like the one below. ...
    (microsoft.public.dotnet.xml)