Extract common properties of a class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Arial

    #1

    Extract common properties of a class

    I have more classes with several common properties.

    e.g.

    class A
    {
    string Class_A_Prop1{g et; set;}
    string Class_A_Prop2{g et; set;}

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

    class B
    {
    string Class_B_Prop1{g et; set;}
    string Class_B_Prop2{g et; 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.Commo nProp1;
    string p2 = entity.CommonPr op2;

    Console.WriteLi ne("{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?


  • Jon Skeet [C# MVP]

    #2
    Re: Extract common properties of a class

    Arial <arial@nospam.c omwrote:
    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 - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Marc Gravell

      #3
      Re: Extract common properties of a class

      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

      Comment

      • Arial

        #4
        Re: Extract common properties of a class


        "Marc Gravell" <marc.gravell@g mail.comwrote in message
        news:85cee558-1d62-4ac2-8f1b-f99945b850e9@34 g2000hsh.google groups.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.(LastChange dTime).



        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.


        Comment

        • Marc Gravell

          #5
          Re: Extract common properties of a class

          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

          Comment

          • Marc Gravell

            #6
            Re: Extract common properties of a class

            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="failureMo de">The action to be taken if the
            submission fails. Valid arguments are as
            follows:System. Data.Linq.Confl ictMode.FailOnF irstConflictSys tem.Data.Linq.C onflictMode.Con tinueOnConflict </
            param>
            public override void
            SubmitChanges(S ystem.Data.Linq .ConflictMode failureMode)
            {
            // Set audit columns correctly
            ChangeSet delta = GetChangeSet();

            IPrincipal principal = Thread.CurrentP rincipal;
            IIdentity identity = principal == null ? null :
            principal.Ident ity;
            string name = identity == null ? null : identity.Name;
            if (string.IsNullO rEmpty(name)) name =
            Environment.Mac hineName;

            foreach (IEntityChangeA udit ins in
            delta.Inserts.O fType<IEntityCh angeAudit>())
            {
            ins.CreatedBy = ins.UpdatedBy = name;
            ins.DateCreated = ins.LastUpdated = DateTime.Now;
            }
            foreach (IEntityChangeA udit upd in
            delta.Updates.O fType<IEntityCh angeAudit>())
            {
            upd.UpdatedBy = name;
            upd.LastUpdated = DateTime.Now;
            }
            base.SubmitChan ges(failureMode );
            }

            Marc

            Comment

            • Arial

              #7
              Re: Extract common properties of a class


              "Marc Gravell" <marc.gravell@g mail.comwrote in message
              news:0d62fa5c-f187-41d4-a294-72ca76743862@r6 6g2000hsg.googl egroups.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.


              Comment

              Working...