Attribute outside Class Definition .cs file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • reviswami78@yahoo.com

    #1

    Attribute outside Class Definition .cs file

    Hi :
    I wanted to know if the following is possible in C# ( .NET Framework
    1.x):
    I want to add an attribute to a class in a separate file i.e. in a file
    other than which the class itself is declared/defined.

    So:

    //File A.cs

    ///////// [TypeConverter(t ypeof(DateConve rter))]
    public class Date : MyDate
    {
    }

    //File B.cs
    public class DateConverter : TypeConverter
    {
    }

    I want to associate the type converter ( using the
    [TypeConverter(t ypeof(DateConve rter))] ) attribute outside in File
    A.cs, perhaps in File B.cs or other file.

    The reason is the File A.cs contains auto-generated code from a custom
    tool, and I dont want the autogenerated code files to be touched by a
    developer. I want the flexibility of replacing the auto-generated code
    by the tool, without losing developer custom additions. Hence I want
    any additions (specifically TypeConverter additions) to be in a
    separate file.

    Is this possible? Any other ideas on how to achieve this?

    Thanks.
    Revi

  • Peter Rilling

    #2
    Re: Attribute outside Class Definition .cs file

    I don't know much about partial classes in C# 2, but you might try using
    partial classes. Although, I think it would need to be marked as partial in
    all files so if the tool generates the definition, it might need to be
    modified to output "partial".

    <reviswami78@ya hoo.com> wrote in message
    news:1134601658 .750307.148190@ z14g2000cwz.goo glegroups.com.. .[color=blue]
    > Hi :
    > I wanted to know if the following is possible in C# ( .NET Framework
    > 1.x):
    > I want to add an attribute to a class in a separate file i.e. in a file
    > other than which the class itself is declared/defined.
    >
    > So:
    >
    > //File A.cs
    >
    > ///////// [TypeConverter(t ypeof(DateConve rter))]
    > public class Date : MyDate
    > {
    > }
    >
    > //File B.cs
    > public class DateConverter : TypeConverter
    > {
    > }
    >
    > I want to associate the type converter ( using the
    > [TypeConverter(t ypeof(DateConve rter))] ) attribute outside in File
    > A.cs, perhaps in File B.cs or other file.
    >
    > The reason is the File A.cs contains auto-generated code from a custom
    > tool, and I dont want the autogenerated code files to be touched by a
    > developer. I want the flexibility of replacing the auto-generated code
    > by the tool, without losing developer custom additions. Hence I want
    > any additions (specifically TypeConverter additions) to be in a
    > separate file.
    >
    > Is this possible? Any other ideas on how to achieve this?
    >
    > Thanks.
    > Revi
    >[/color]


    Comment

    • reviswami78@yahoo.com

      #3
      Re: Attribute outside Class Definition .cs file

      I think partial classes is a feature newly introduced only in .NET 2.0.
      I am targeting and supporting .NET 1.x code.

      Any ideas on how this can be achieved in .NET 1.x

      Comment

      • Marc Gravell

        #4
        Re: Attribute outside Class Definition .cs file

        Not 100% sure if it works, but in .Net 2, if you define the class as partial
        in both places it seems to at least accept the attributes from 2
        declarations... note that this will only allow you to a: add new attributes
        to the class definition and b: add new methods / properties etc - you won't
        be able to change functionality already defined in a separate class.

        example with 2 random attributes (note it doesn't matter whether these two
        are in separate files, the same file, or whatever)

        [Serializable]
        public partial class A {
        public void FuncA() { }
        }
        [CLSCompliant(tr ue)]
        public partial class A {
        public static void FuncB() { }
        }

        is equivalent to

        [Serializable, CLSCompliant(tr ue)]
        public class A {
        public void FuncA() { }
        public static void FuncB() { }
        }

        Hope this helps,

        Marc


        <reviswami78@ya hoo.com> wrote in message
        news:1134601658 .750307.148190@ z14g2000cwz.goo glegroups.com.. .[color=blue]
        > Hi :
        > I wanted to know if the following is possible in C# ( .NET Framework
        > 1.x):
        > I want to add an attribute to a class in a separate file i.e. in a file
        > other than which the class itself is declared/defined.
        >
        > So:
        >
        > //File A.cs
        >
        > ///////// [TypeConverter(t ypeof(DateConve rter))]
        > public class Date : MyDate
        > {
        > }
        >
        > //File B.cs
        > public class DateConverter : TypeConverter
        > {
        > }
        >
        > I want to associate the type converter ( using the
        > [TypeConverter(t ypeof(DateConve rter))] ) attribute outside in File
        > A.cs, perhaps in File B.cs or other file.
        >
        > The reason is the File A.cs contains auto-generated code from a custom
        > tool, and I dont want the autogenerated code files to be touched by a
        > developer. I want the flexibility of replacing the auto-generated code
        > by the tool, without losing developer custom additions. Hence I want
        > any additions (specifically TypeConverter additions) to be in a
        > separate file.
        >
        > Is this possible? Any other ideas on how to achieve this?
        >
        > Thanks.
        > Revi
        >[/color]


        Comment

        • reviswami78@yahoo.com

          #5
          Re: Attribute outside Class Definition .cs file

          This would work, as you said with .NET 2.0.
          I am using .NET 1.1, and need to support that version.

          Any ideas?

          Comment

          • Ivan Wong

            #6
            RE: Attribute outside Class Definition .cs file

            I don't think there is a way to do it in .NET 1.x
            and i don't think you can do it in .NET 2.0 neither
            <i assume you wanna change the attribute at runtime :p>
            unless you create the whole class at runtime using Reflection.Emit .

            Hope it helps,
            Ivan Wong

            "reviswami78@ya hoo.com" wrote:
            [color=blue]
            > Hi :
            > I wanted to know if the following is possible in C# ( .NET Framework
            > 1.x):
            > I want to add an attribute to a class in a separate file i.e. in a file
            > other than which the class itself is declared/defined.
            >
            > So:
            >
            > //File A.cs
            >
            > ///////// [TypeConverter(t ypeof(DateConve rter))]
            > public class Date : MyDate
            > {
            > }
            >
            > //File B.cs
            > public class DateConverter : TypeConverter
            > {
            > }
            >
            > I want to associate the type converter ( using the
            > [TypeConverter(t ypeof(DateConve rter))] ) attribute outside in File
            > A.cs, perhaps in File B.cs or other file.
            >
            > The reason is the File A.cs contains auto-generated code from a custom
            > tool, and I dont want the autogenerated code files to be touched by a
            > developer. I want the flexibility of replacing the auto-generated code
            > by the tool, without losing developer custom additions. Hence I want
            > any additions (specifically TypeConverter additions) to be in a
            > separate file.
            >
            > Is this possible? Any other ideas on how to achieve this?
            >
            > Thanks.
            > Revi
            >
            >[/color]

            Comment

            • Daniel Jin

              #7
              Re: Attribute outside Class Definition .cs file



              "reviswami78@ya hoo.com" wrote:
              [color=blue]
              > I think partial classes is a feature newly introduced only in .NET 2.0.
              > I am targeting and supporting .NET 1.x code.
              >
              > Any ideas on how this can be achieved in .NET 1.x
              >
              >[/color]

              you can't. this is exactly what partial type is designed to address.
              allowing you to combine code generation with hand customization easily.

              in 1.x, the closest thing that can help you 'simulate' a partial type-ish
              feel is through inheritance. hand customized class inheriting from code
              generated base. doesn't work as well as partial types though. I would
              strongly suggest migrate to 2.0 if you can to solve your problem.

              Comment

              • reviswami78@yahoo.com

                #8
                Re: Attribute outside Class Definition .cs file

                Could you elaborate on th Reflection.Emit approach. If I would be
                emitting a NEW type and not replacing the earlier one, how would this
                be different from a another class simply inheriting the auto-generated
                class (which is not really a suitable solution)

                Some details on the Reflection.Emit approach would be great.

                Comment

                • Ivan Wong

                  #9
                  Re: Attribute outside Class Definition .cs file

                  Sorry, I should have read your question probably before i answer your question.
                  (i miss the "auto generated" code file part).
                  I don't think there is a way to do it in .NET 1.1.

                  Hope it helps,
                  Ivan Wong

                  "reviswami78@ya hoo.com" wrote:
                  [color=blue]
                  > Could you elaborate on th Reflection.Emit approach. If I would be
                  > emitting a NEW type and not replacing the earlier one, how would this
                  > be different from a another class simply inheriting the auto-generated
                  > class (which is not really a suitable solution)
                  >
                  > Some details on the Reflection.Emit approach would be great.
                  >
                  >[/color]

                  Comment

                  Working...