Yes. You can use Aspect Oriented Programming technique for this. Here is the solution when you use PostSharp. PostSharp can be installed via nuget (postsharp express is free).
You need to write an aspect first. See the code below.
namespace AspectOrientedProgramming
{
[Serializable]
[MulticastAttributeUsage (MulticastTargets.Class)]
public sealed class DataContractAspect:TypeLevelAspect, IAspectProvider
{
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
var targetType = (Type) targetElement;
var introduceDataContractAspect =
new CustomAttributeIntroductionAspect(
new ObjectConstruction(typeof(DataContractAttribute).GetConstructor(Type.EmptyTypes)));
var introduceDataMemberAspect =
new CustomAttributeIntroductionAspect(
new ObjectConstruction(typeof(DataMemberAttribute).GetConstructor(Type.EmptyTypes)));
yield return new AspectInstance(targetType, introduceDataContractAspect);
foreach (var property in targetType.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance))
{
if (property.CanWrite && !property.IsDefined(typeof(NotADataMemberAttribute), false))
{
yield return new AspectInstance(property, introduceDataMemberAspect);
}
}
}
}
}
Now create a Not a datamember attribute.
[AttributeUsage(AttributeTargets.Property)]
public sealed class NotADataMemberAttribute:Attribute
{
}
Decorate those few properties that you don't want to be DataMembers with [NotADataMember].
Add the following line in AssemblyInfo.cs file. Replace YourNameSpaces with appropriate to your project specific.
[assembly: DataContractAspect(AttributeTargetTypes = "YourNamespaces.DataContracts.*")]
Thats it.
With this approach, You don't have to decorate every member with DataMember attribute. Also, you don't have to decorate every class with DataContract.