Title: Some Advanced Programming Techniques and Tips Part 1
1Some Advanced Programming Techniques and Tips
(Part 1)
2Well cover
- Using Meta-Data and Reflection
- Types are First Class Objects
- A GUI example - the PropertyGrid editor
3.Net internals similar to Java
4Key differences
- Multi-language, all use same intermediate code,
- All use same .NET base data types,
- Very easily mixable, any project can be in any
.NET language, they can inherit from each other,
throw an Exception in one module written in
Eiffel, catch it in another module written in C,
etc. - About 23 languages at this writing
5.NET and C vs Java
- .NET framework and libraries have leapfrogged
Java in many areas - Important ones are perhaps XML facilities, Web
Services, networking facilities, better GUI,
nicer interoperability between languages and to
legacy unmanaged code, and better metadata.
6Meta-data and Reflection
- Important advance is putting much more meta-data
into compiled object files and libraries. - An external tool can load a compiled class and
perform run-time reflection learn about the
structure (members, methods, properties, types,
parameters, modifiers like private, public,
protected ) of the class,
7Reflection and meta-data
- Allows external tools like debuggers, NUnit,
profilers, etc. to do magic things that are not
otherwise possible, - e.g. influence what XML tags should be used when
serializing an object into XML, or whether a
given property should become an XML attribute
rather than a value node in the XML tree
8You can define your own meta-data
- C decorates objects with meta-data using
Attributes. - A new tool (like NUnit) can define its own
attributes for decorating objects, e.g. - TestFixture class X
- Test private void t01()
- The NUnit runtime can later find this meta-data
in the complied object and act accordingly.
9A little meta-data can go a long way
WebMethod public dataSet getUser(string uid)
10Meta-data is declarative
- Declarative What, rather than How To
- Easier (well, especially if you believe the
functional programmers).
11A declarative way to make a web method into a
Transaction
WebMethod (TransactionOption
TransactionOption.RequiresNew) public void
DeleteAuthor(string lastName)
SqlCommand xx new (DELETE ltsome recordgt ,
) SqlCommand yy new
(EXCEPTION-CAUSING BAD SQL)
xx.ExecuteNonQuery() // this succeeds
yy.ExecuteNonQuery() // but this fails, so the
// whole transaction is rolled back.
12Types are objects, too!
- In modern programming theory (in Java, .NET
framework, etc) a Type is also an object. So
when your Virtual Machine learns about a type
like String (we call it loading the Type) it
creates an instance object. - So if you have three String objects allocated,
there are really 4 objects one for the Type t
typeof(String) and three instances.
13Because a Type is an object
- It has members, methods, properties that can be
called at run time. - e.g. you can ask a Type object t to
- return a list of its members, t.
GetMembers() - or to tell you about its metadata.
t.GetCustomAttributes()
14A GUI example
- The PropertyGrid control can probe into an
object, and provide an easy way to edit the
objects public properties
15Plugging a Problem into a PropertyGrid
16Extra Attributes for PropertyGrid editing
- You can decorate each property in your object
with some new attributes to influence how the
property grid treats the property when it edits - Make property read-only,
- Dont show it in the property grid,
- Add a description/help to be shown when selected,
- Use a special dialog for editing this property,
- Categorize the properties,
- Use default values for each property,
17Editing more complex types
- You can attach your own sub-editor to a class
via an Attribute, so that attempts to edit a
property which is an instance of your class will
pop up a special dialog (e.g. a color dialog
picker). - So because the framework already defines some
sub-editors for its library types - enum types create dropdown boxes,
- Colors edit with a nice dialog
- Lists / collections of items are editable
18(No Transcript)
19You can try this too
- Type t typeof(String)
- propertyGrid1.SelectedObject t
- and youll get some idea of what the object t
looks like.
20Conclusion
- When you design a class, ask the question
- Do we want to pay some extra price now and add
some attributes to our properties so that
PropertyGrid editing works nicely on our
objects... - A good solution to a problem might involve
creating your own attributes, and writing a tool
to do something special by using reflection and
the meta-data in your objects. - Attributes may be a cleaner way to express some
aspect of your object declaratively.