Jump to content
  • Advertisement
Sign in to follow this  
JimPrice

[.net] Attributes / Design patterns

This topic is 5006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've recently started reading up on C#; simultaneously working my way through 3 books - Microsoft Visual C#.Net, Design Patterns in C#, and Managed DirectX 9. I have to say - so far I'm pretty impressed. Coming from a background in mathematical programming (and having been introduced to direct memory management for the first time this year when I started learning C++), I really like the whole C# language. Anyways, a quick few questions. As I go on I'm trying to relate what I read to my current code-base (as far as possible), which is ostensibly C++ (including Boost and Loki), Win32 and OpenGL. So - here goes: Attributes - OK, I get the idea - but what do you actually use them for in practice? Singletons - Singletons are simply implemented in C++, using inheritance from a base-class template (ie Enginuity / GPG), or sticking a class into a seperate pattern tempate (Loki). Is there a simple way to do this in C#, or do you need to add the whole code on a per-class basis? Registration - For example, with object factories. My current resource manager is a single point of contact for all resource requests. At set-up, each seperate manager registers itself with the RM (using an anonymous namespace containing global variables) - this allows me to retain header-independence (which isn't an issue for C#), but, more importantly, allows me to slot in the required managers for any given project just by adding / removing files. The question is : how do I register one class with another in C# in the same way? Thanks for any help, Jim.

Share this post


Link to post
Share on other sites
Advertisement
Attributes: You use them when you want to add extra information about the code to the assembly. The extra information can be anything you like/need.

Singletons: You are right; you'll have to add this code to each class again. However in .NET 2.0/C# 2.0 you can use generics to get the template effect.

Registration: There are several options. The one I use is as follows: define an interface (or base class) and then use Activator.CreateInstance

See this for an example I wrote for Database connectivity.

Cheers

Share this post


Link to post
Share on other sites
On Attributes:
Attributes are really cool, being able to specify arbitrary metadata on classes, methods or whatever opens up all sorts of doors. You can use some of the provided attributes to control how things compile and interop, but these are really just like special keywords. When they get really cool is when you use them with reflection to work with objects and methods that you may not know much about.

The PropertyGrid is a great example. Using it we can generate an windows forms based editor for an object that we know almost nothing about. We can use the attributes from the ComponentModel namespace to control several aspects of the editor. What's more we are not tied to the PropertyGrid, we can use the same metadata to create a web interface or to drive our own custom interface.

Using custom attributes we can insert extra levels of control, complicated signature patterns and management instructions all terribly usefull when working with somehting like plugins where we may need to gather information about an assembly that came from another party. For example an Assembly Attributes could tell us what features the plugin provides, whaere to locate additional resources, address compatibility issues, and even how to contact the 3rd party. Mehtod/Property attributes could tell us about data pre-validation and interface presentation.

This is just scratchig the surface, no doubt there are countless outher uses.

Share this post


Link to post
Share on other sites
Thanks for the responses. Much appreciated. Haven't come across generics yet (and they're not in any of my current books) - need to start browsing the ole' web.

Keep 'em coming!
Jim.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!