Attribute outside Class Definition .cs file

R

reviswami78

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
 
P

Peter Rilling

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".
 
R

reviswami78

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
 
M

Marc Gravell

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
 
R

reviswami78

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

Any ideas?
 
G

Guest

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
 
G

Guest

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.
 
R

reviswami78

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.
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top