473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Extract common properties of a class

I have more classes with several common properties.

e.g.

class A
{
string Class_A_Prop1{get; set;}
string Class_A_Prop2{get; set;}

string CommonProp1{get; set;} // common to all classes
string CommonProp2{get; set;}
}

class B
{
string Class_B_Prop1{get; set;}
string Class_B_Prop2{get; set;}

string CommonProp1{get; set;} // common to all classes
string CommonProp2{get; set;}
}

i want to write generic method (without using reflection)
which will display properties CommonPorp1 and CommonProp2

something like this

void DisplayCommon<T>(T entity)
{

string p1=entity.CommonProp1;
string p2 = entity.CommonProp2;

Console.WriteLine("{0} {1}",p1,p2);
}

keep in mind that class A & B are generated by a external tool
and I do not want to alter them in any way,
so forget solutions that are using abstract classes, inheritance and
interfaces

is this possible in C# using generics or anonymous types?
Sep 24 '08 #1
6 1821
Arial <ar***@nospam.comwrote:
I have more classes with several common properties.
<snip>
keep in mind that class A & B are generated by a external tool
and I do not want to alter them in any way,
so forget solutions that are using abstract classes, inheritance and
interfaces

is this possible in C# using generics or anonymous types?
No. If there's no interface which explains that A.CommonProp1 and
B.CommonProp1 are linked, you'll have to use reflection.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 24 '08 #2
keep in mind that class A & B are generated by a external tool

Are they (or could they be) declared as partial classes? If this was
"partial class A" etc, then you could use a *second* code file to make
A & B implement a common interface:

interface IFoo {...} // common members
partial class A : IFoo {}
partial class B : IFoo {}

And let implicit interface implementation deal with the rest (assuming
the properties etc are public). Then your external tool doesn't ever
know about IFoo.

If this isn't an option, then reflection is your only choice.

Marc
Sep 24 '08 #3

"Marc Gravell" <ma**********@gmail.comwrote in message
news:85**********************************@34g2000h sh.googlegroups.com...
>keep in mind that class A & B are generated by a external tool

Are they (or could they be) declared as partial classes? If this was
"partial class A" etc, then you could use a *second* code file to make
A & B implement a common interface:

interface IFoo {...} // common members
partial class A : IFoo {}
partial class B : IFoo {}

And let implicit interface implementation deal with the rest (assuming
the properties etc are public). Then your external tool doesn't ever
know about IFoo.

If this isn't an option, then reflection is your only choice.

Marc
Very clever idea Marc.

In fact those files are generated by SQLMetal.

and common properties represents fields from db which are the same in every
table

e.g.(LastChangedTime).

The problem is that there are a lot of those classes, and reflection seems
to be only painless and satisfactory solution in my case.

Anyway I appreciate your wise answer.
Sep 24 '08 #4
In that case, simply specify the interface name as the base-class; for
dbml files this is an entry in the xml file. At the command line, this
is /entitybase

I use *exactly* this (the dbml version) to define a common audit
interface (who/when created/updated), and I override the
GetChangeSet() [or something] so that the "who" gets updated
automatically to the current user (using the thread's principal).

Marc
Sep 24 '08 #5
Sorry, it is SubmitChanges:

/// <summary>
/// Sends changes that were made to retrieved objects to the
underlying database,
/// and specifies the action to be taken if the submission
fails.
/// </summary>
/// <param name="failureMode">The action to be taken if the
submission fails. Valid arguments are as
follows:System.Data.Linq.ConflictMode.FailOnFirstC onflictSystem.Data.Linq.ConflictMode.ContinueOnCon flict</
param>
public override void
SubmitChanges(System.Data.Linq.ConflictMode failureMode)
{
// Set audit columns correctly
ChangeSet delta = GetChangeSet();

IPrincipal principal = Thread.CurrentPrincipal;
IIdentity identity = principal == null ? null :
principal.Identity;
string name = identity == null ? null : identity.Name;
if (string.IsNullOrEmpty(name)) name =
Environment.MachineName;

foreach (IEntityChangeAudit ins in
delta.Inserts.OfType<IEntityChangeAudit>())
{
ins.CreatedBy = ins.UpdatedBy = name;
ins.DateCreated = ins.LastUpdated = DateTime.Now;
}
foreach (IEntityChangeAudit upd in
delta.Updates.OfType<IEntityChangeAudit>())
{
upd.UpdatedBy = name;
upd.LastUpdated = DateTime.Now;
}
base.SubmitChanges(failureMode);
}

Marc
Sep 24 '08 #6

"Marc Gravell" <ma**********@gmail.comwrote in message
news:0d**********************************@r66g2000 hsg.googlegroups.com...
In that case, simply specify the interface name as the base-class; for
dbml files this is an entry in the xml file. At the command line, this
is /entitybase

I use *exactly* this (the dbml version) to define a common audit
interface (who/when created/updated), and I override the
GetChangeSet() [or something] so that the "who" gets updated
automatically to the current user (using the thread's principal).

Marc
Tnx a lot!
That is exactley what I need.
Sep 24 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: D Witherspoon | last post by:
What is the accepted method of creating a data class or business rules object class with properties that will allow the returning of null values? For example... I have a class named CResults with...
3
by: Gerry Abbott | last post by:
Access allows all objects, tables, forms, queries, macros, modules, to be documented with common object properties, including Description, Type, modified and created dates, owner, and some...
3
by: CaribSoft | last post by:
I want to create my own common dialog to use in an application . How do I show a custom dialog (form) in a class based on the common dialog class?
2
by: John B | last post by:
Hello I want to create a set of controls that have some common methods and properties. I was thinking that I should subclass the UserControl class and add these common features there and then...
4
by: Sharon | last post by:
Hi guys I don't understand what I am doing wrong I wrote this code, in order to manipulate some Xml data, I have problem with level 4 (properties section): //CODE...
3
by: Garth17 | last post by:
I'm trying to figure out a solution for sharing common properties and methods in all me .aspx and .ascx pages. In classic ASP I would use include directives. So far I have made 2 base classes...
5
by: wrecker | last post by:
Hi all, I have a few common methods that I need to use at different points in my web application. I'm wondering where the best place would be to put these? I think that I have three options. ...
10
by: Pavel Shved | last post by:
Is there a program of wide use that automatically extracts template parameters requirements from source code of template functions? I mean the following: assume we have a template function that...
1
by: =?Utf-8?B?SkI=?= | last post by:
Hello My pgm1 (User Interface Level) passes an empty ArrayList to pgm2 (Business Logic Level). pgm2 then calls pgm3 (Data Access Level) to populate the ArrayList. Question1: When pgm2 gets...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.