Adding Attributes (or equivalent effect) Without Touching Original Source Code
- From: "Jordan S" <A@xxxxx>
- Date: Mon, 26 Jan 2009 22:52:52 -0800
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!
.
- Follow-Ups:
- Prev by Date: Re: Object with two base classes
- Next by Date: Re: Object with two base classes
- Previous by thread: Object with two base classes
- Next by thread: Re: Adding Attributes (or equivalent effect) Without Touching Original Source Code
- Index(es):
Relevant Pages
|