Creating my own attribute

neodatatype

Regular
Joined
Aug 18, 2003
Messages
65
Location
Italy
Hi all!

I wonder how can I create my own class sttribute in C#.

My aim is to "wrap" some attributes now I use for each class into a single attribute.

Is possible to do this?
How?

Thank you all
 
Yep. We need more info on what you are wrapping. Here is a Wrapped Bitmap:
C#:
[Serializable]
			public class WrappedBmp
			{
				public Bitmap Bmp;
				public readonly Bitmap OriginalBmp;
				public Rectangle Bounds;
				public Rectangle ClipBounds;
				public string Name;
				public int Number;
				public int TranspLevel= 0;
				public bool Selectable=true;

				public WrappedBmp(Bitmap b, Rectangle bounds, int transpLevel, int number)
				{
					this.Bmp= b;
					this.OriginalBmp= new Bitmap(b);
					this.Bounds= bounds;
					this.ClipBounds= new Rectangle(0,0, bounds.Width, bounds.Height);
					this.Number= number;
					this.TranspLevel= transpLevel;
				}

				public void RestoreOriginalBmp()
				{
					this.Bmp=new Bitmap(this.OriginalBmp);
				}
			}
Serializable just means that it can be placed in memory and used as a data type.
 
aewarnick said:
Yep. We need more info on what you are wrapping.

woa! superspeedy :)

Ok, let's tell I have my class, with 2 attributes like this:

C#:
[ FirtstAttribute(FirstAttributeParam) ]
[ SecondAttribute(SecondAttributeParam) ]
public class MyClass{}

I'd like to do something like

C#:
// Pseudo-code
MyAttribute = FirtstAttribute + SecondAttribute;

// C#-code
[ MyAttribute(FirstAttributeParam, SecondAttributeParam) ]
public class MyClass{}
 
You C# context is wrong. You cannot put just anything in the [] above the class. It is only for special things.

This may be what you want:

C#:
public class MyClass
{
    public MyClass(type a, type b, type c) //constructor- called when the class is created.
    {

    }

    public MyClass(type b) //constructor- can be many more if you want.
    {

    }
}
 
Here is another example- my own form which inherits from Form:
C#:
/// <summary>
			/// Inherits from Form and has additional features including the ability to center an image on the form at all times with transparency if you want. You can also paint the background with any brush you want.
			/// </summary>
			public class aForm : Form
			{

				Brush backgrdBrush=null;
				Image backgrdImage=null;
				int backgrdImageTransp=100;
				bool controlsTransp=true;
				bool controlsTranspInclBut=false;

				/// <summary>
				/// If some controls are not being colored right you may be able to figure out why.
				/// </summary>
				public string Errors="Check to see if the FormBorderStyle is set to 3D.\n\n";

				/// <summary>
				/// A brush to paint the background any color you want. Will always be behind the image.
				/// </summary>
				public Brush BackgrdBrush
				{
					get{return backgrdBrush;}
					set{backgrdBrush=value; this.Invalidate();}
				}

				/// <summary>
				/// Places an image in the center of the form keeping its proportions.
				/// </summary>
				public Image BackgrdIm
				{
					get{return backgrdImage;}
					set{backgrdImage=value; this.Invalidate();}
				}

				/// <summary>
				/// Sets the transparency percent to the backgrdImage.  0 is clear.  If the image is not transparent at all the painting will draw faster: 100.
				/// </summary>
				public int BackgrdImTransp
				{
					get{return backgrdImageTransp;}
					set{backgrdImageTransp=value; this.Invalidate();}
				}

				/// <summary>
				/// Set to false if you want the controls on the form to take on the BackColor of the form.
				/// </summary>
				public bool ControlsTransp
				{
					get{return this.controlsTransp;}
					set{this.controlsTransp=value; this.Invalidate();}
				}

				/// <summary>
				/// You want your buttons to be transparent too.
				/// </summary>
				public bool ControlsTranspInclBut
				{
					get{return this.controlsTranspInclBut;}
					set{this.controlsTranspInclBut=value; this.Invalidate();}
				}

				//------------------------
				public aForm()
				{
					this.SetStyle(ControlStyles.DoubleBuffer, true);
					this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
					this.SetStyle(ControlStyles.UserPaint, true);

					this.BackColor= a.Colors.aControl;
				}
				//------------------------

				protected override void OnPaint(PaintEventArgs e)
				{
					e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
					base.OnPaint(e);

					if( (controlsTransp || controlsTranspInclBut) && (this.backgrdBrush != null || this.backgrdImage != null) )
					{
						if(controlsTranspInclBut)
						{
							foreach(Control c in this.Controls)
							{
								try
								{
									if(c is Label || c is GroupBox || c is Panel || c is LinkLabel || c is Button || c is CheckBox || c is RadioButton)
										((Control)c).BackColor= Color.FromArgb(0,0,0,0);
									/*else if(c is TabControl)
									{
										foreach(TabPage tp in ((TabControl)c).TabPages)
											tp.BackColor= Color.FromArgb(0,0,0,0);
									}*/
								}
								catch(Exception er) { this.Errors+= er+"\n\n"+c+"\n\n"; }
							}
						}
						else
						{
							ArrayList buttonsList= new ArrayList(2);
							ArrayList buttonsColorList= new ArrayList(2);

							foreach(Control c in this.Controls)
							{
								try
								{
									if(c is Label || c is GroupBox || c is Panel || c is LinkLabel || c is CheckBox || c is RadioButton)
										((Control)c).BackColor= Color.FromArgb(0,0,0,0);
							
									else if(c is TabControl)
									{
										foreach(TabPage tp in ((TabControl)c).TabPages)
										{
											foreach(Control tc in tp.Controls)
											{
												if(tc is Button)
												{
													buttonsList.Add(tc);
													buttonsColorList.Add(tc.BackColor);
												}
											}

											tp.BackColor= Color.FromArgb(0,0,0,0);
									
											for(int i=0; i<buttonsList.Count; i++)
											{
												((Button)buttonsList[i]).BackColor= ((Color)buttonsColorList[i]);
											}
										}
									}
								}
								catch(Exception er) { this.Errors+= er+"\n\n"+c+"\n\n"; }
							}
						}
					}

					try
					{
						if(this.backgrdBrush != null)
						{
							//LinearGradientBrush LGB=new LinearGradientBrush(this.backgrdBrush.FillStartEndArea, this.backgrdBrush.Color1, this.backgrdBrush.Color2, this.backgrdBrush.Rotation);
							e.Graphics.FillRectangle(this.backgrdBrush, this.ClientRectangle);
						}

						if(this.backgrdImage != null)
						{
							Size ImSize= a.graphics.ProportionalSize(backgrdImage.Size, new Size(this.ClientRectangle.Width, this.ClientRectangle.Height));

							Rectangle centerR= a.graphics.CenterRect(this.ClientRectangle, ImSize);

							if(this.backgrdImageTransp != 100)
							{
								ImageAttributes IA = a.graphics.TrasparentImage(backgrdImageTransp);
								e.Graphics.DrawImage(backgrdImage, centerR, 0, 0, backgrdImage.Size.Width, backgrdImage.Size.Height, GraphicsUnit.Pixel, IA);
							}
							else
								e.Graphics.DrawImage(backgrdImage, centerR, 0, 0, backgrdImage.Size.Width, backgrdImage.Size.Height, GraphicsUnit.Pixel);
								//e.Graphics.DrawImageUnscaled(backgrdImage, 2, 2);
						}
					}
					catch(Exception er) { this.Errors += er+"\n\n"; }
				}
				//-------------------
				protected override void OnResize(EventArgs e)
				{
					base.OnResize(e);

					if(this.backgrdBrush != null || this.backgrdImage != null)
						this.Invalidate();
				}
			
			}
 
Another Form who's only purpose is to display a picture:
C#:
public class PictureForm : Form
			{
				Bitmap picture=null;
				public Bitmap Picture
				{
					get{return picture;}
					set{picture=value; this.ClientSize=new Size(this.picture.Width, this.picture.Height);}
				}

				public PictureForm(Bitmap pic)
				{
					Initialize();
					Picture= pic;
				}

				public PictureForm()
				{
					Initialize();
				}

				void Initialize()
				{
					this.SetStyle(ControlStyles.DoubleBuffer, true);
					this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
					this.SetStyle(ControlStyles.UserPaint, true);

					this.Text="Picture Form";
				}

				protected override void OnPaint(PaintEventArgs e)
				{
					base.OnPaint(e);

					if(this.picture != null)
					{
						e.Graphics.DrawImageUnscaled(this.picture, 0,0, this.picture.Width, this.picture.Height);
					}
				}
			}
 
You cannot put just anything in the [] above the class. It is only for special things.

I know it!
Infact I told you about Attributes, that are that "stpecial things" between the [].

[/QUOTE]
This may be what you want:

C#:
public class MyClass
{
    public MyClass(type a, type b, type c) //constructor- called when the class is created.
    {

    }

    public MyClass(type b) //constructor- can be many more if you want.
    {

    }
}[/QUOTE] 

No, I don't speak about inheritance or poliphormism.

I just want to create and use my own attribute (with "attribute" i mean that the word "attribute" means to .NET).

See you.
 
You mean this kind of stuff:
C#:
public Brush BackgrdBrush
{
     get{return backgrdBrush;}
     set{backgrdBrush=value; this.Invalidate();}
}
 
To combine existing attributes into one... I don't think you can. Or if you can, it's probably not worth the effort. I'm guessing you're looking for a simple way to cut and paste attributes of classes/members? If not, let us know how else you're using them and maybe we can help.

-Ner
 
To combine existing attributes into one... I don't think you can. Or if you can, it's probably not worth the effort. I'm guessing you're looking for a simple way to cut and paste attributes of classes/members? If not, let us know how else you're using them and maybe we can help.

You have exactly understanded what I mean.

Bye
 
An attribute is just like any other class, and in this way you
could create some fields in the class that contain all the attributes
you want to stick together, and just create a special constructor(s)
that allows you initailize all them properly.

Then, to query all these attributes with reflection, you could just
cast the attribute into a variable and access its fields (the other
attributes).

Does that make sense? Am I completely missing the point of
what you're trying to do?
 
Back
Top