471,327 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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(typeof(DateConverter))]
public class Date : MyDate
{
}

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

I want to associate the type converter ( using the
[TypeConverter(typeof(DateConverter))] ) 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

Dec 14 '05 #1
8 2279
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".

<re*********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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(typeof(DateConverter))]
public class Date : MyDate
{
}

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

I want to associate the type converter ( using the
[TypeConverter(typeof(DateConverter))] ) 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

Dec 14 '05 #2
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

Dec 14 '05 #3
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(true)]
public partial class A {
public static void FuncB() { }
}

is equivalent to

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

Hope this helps,

Marc
<re*********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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(typeof(DateConverter))]
public class Date : MyDate
{
}

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

I want to associate the type converter ( using the
[TypeConverter(typeof(DateConverter))] ) 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

Dec 14 '05 #4
This would work, as you said with .NET 2.0.
I am using .NET 1.1, and need to support that version.

Any ideas?

Dec 14 '05 #5
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

"re*********@yahoo.com" wrote:
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(typeof(DateConverter))]
public class Date : MyDate
{
}

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

I want to associate the type converter ( using the
[TypeConverter(typeof(DateConverter))] ) 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

Dec 15 '05 #6


"re*********@yahoo.com" wrote:
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


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.
Dec 15 '05 #7
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.

Dec 15 '05 #8
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

"re*********@yahoo.com" wrote:
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.

Dec 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Harry F. Harrison | last post: by
1 post views Thread by Nick Bassiliades | last post: by
3 posts views Thread by Tony Johansson | last post: by
reply views Thread by rosydwin | last post: by

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.