Component Programming
with C# and .NET
1st Class Component Support
Robust and Versionable
Creating and using attributes
API integration
DLL import
COM support
Preserve Existing Investments
AP 08/01
What defines a component?
What defines a component?
Properties, methods, events
Design-time and runtime information
Integrated help and documentation
C# has first class support
Not naming patterns, adapters, etc.
Not external files
Easy to build and consume
AP 08/01
Properties
Properties are smart fields
Natural syntax, accessors, inlining
public class Button: Control
{
private string caption;
public string Caption {
get {
return caption;
}
set {
caption = value;
Repaint();
}
}
}
Button b = new Button();
b.Caption = "OK";
String s = b.Caption;
AP 08/01
Indexers
Indexers are smart arrays
Can be overloaded
public class ListBox: Control
{
private string[] items;
public string this[int index]{
get {
return items[index];
}
set {
items[index] = value;
Repaint();
}
}
}
ListBox listBox = new ListBox();
listBox[0] = "hello";
Console.WriteLine(listBox[0]);
AP 08/01
Events
Efficient, type-safe and customizable
Built on delegates
public class MyForm: Form {
public MyForm()
{
Button okButton = new Button(...);
okButton.Click += new EventHandler(OkButtonClick);
}
void OkButtonClick()
{
ShowMessage("You clicked OK");
}
}
AP 08/01
Design/Runtime Information
Add information to types + methods?
Transaction required for a method?
Mark members for persistence?
Default event hookup?
Traditional solutions
Lots of custom user code
Naming conventions between classes
External files (e.g. .IDL, .DEF)
The C# solution - Attributes
AP 08/01
Attributes
Appear in square brackets
Attached to code elements
[HelpUrl(http://SomeUrl/Docs/SomeClass)]
class SomeClass
{
[WebMethod]
void GetCustomers() { }
string Test([SomeAttr] string param1) {}
}
AP 08/01
Attribute Fundamentals
Attributes are classes! Completely generic
class HelpUrl : System.Attribute {
public HelpUrl(string url) { }
}
[HelpUrl(http://SomeUrl/APIDocs/SomeClass)]
class SomeClass { }
Easy to attach to types and members
Type type = Type.GetType(SomeClass);
object[] attributes =
type.GetCustomAttributes();
Attributes can be queried at runtime
AP 08/01
Attributes in .NET
Web Services
COM Interop
Platform Invoke (DLL Interop)
Transaction Contexts
Permissions
Custom XML Persistence
User-defined attributes to specify non-functional
component properties (RT / FT / Security / Config. )
AP 08/01
XML Comments
XML schema for comments
Method description
Parameter types, names and descriptions
Add your own tags - just XML
Compiler and IDE support
Compiler creates XML file for all classes
IDE support for entry and display
Used throughout .NET Framework
AP 08/01
Using Attributes
[HelpUrl(http://SomeUrl/APIDocs/SomeClass)]
class SomeClass
{
[Obsolete(Use SomeNewMethod instead)]
public void SomeOldMethod()
{}
public string Test( [SomeAttr()] string param1 )
{}
AP 08/01
Using Attributes (contd.)
[HelpUrl("http://SomeUrl/MyClass")]
class MyClass {}
[HelpUrl("http://SomeUrl/MyClass, Tag=ctor)]
class MyClass {}
[HelpUrl("http://SomeUrl/MyClass"),
HelpUrl("http://SomeUrl/MyClass, Tag=ctor)]
class MyClass {}
AP 08/01
Querying Attributes
Type
Use
reflection
to query attributes
type
= typeof(MyClass);
foreach(object attr in type.GetCustomAttributes() )
{
if ( attr is HelpUrlAttribute )
{
HelpUrlAttribute ha = (HelpUrlAttribute) attr;
myBrowser.Navigate( ha.Url );
AP 08/01
Replication based on Attributes
Specification of a components
non-functional properties at
namespace CalculatorClass {
design time
using System; using proxy;
A tool may generate code to
automatically create replicated
[TolerateCrashFaults(4)]
objects
public class Calculator {
Component behavior described
...
by user-defined attributes
public double add
(double x, double y) {
return x + y;
}
}
}
AP 08/01
Behind the scenes...
public sealed class Calculator:CalculatorClass.Calculator {
private CalculatorClass.Calculator[] _bc;
public Calculator(): base() {
Reflection
_ErrorCount=0; int _Count=1;
System.Attribute[] _arAtt =
System.Attribute.GetCustomAttributes(GetType());
foreach(System.Attribute _attr in _arAtt) {
Attribute-based
if(_attr is TolerateCrashFaults)
programming
_Count=((TolerateCrashFaults)_attr).Count;
}
/// creation of sufficiently many proxy objects
_bc=new CalculatorClass.Calculator[_Count];
AP 08/01
From the consumers perspective
namespace CalculatorFront {
using System;
///
using
CalculatorClass;
using
CalculatorClass;
using proxy;
Minimal code
changes
public class MainClass {
public MainClass() {}
public static int Main(string[] args) {
Calculator calc = new Calculator();
...
result = calc.add(val1, val2);
AP 08/01
Calling Into Existing DLLs
.NET Framework contains attributes to enable calling
into existing DLLs
System.Runtime.InteropServices
DLL Name, Entry point, Parameter and Return value marshalling, etc.
Use these to control calling into your existing DLLs
System functionality is built into Framework
AP 08/01
Attributes to specify DLL Imports
[DllImport("gdi32.dll")]
public static extern
int CreatePen(int style, int width, int color);
[DllImport("gdi32.dll",CharSet=CharSet.Auto)]
public static extern
int GetObject( int hObject,
int nSize,
[In, Out] ref LOGFONT lf);
AP 08/01
COM Support
.NET Framework provides great
COM support
TLBIMP imports existing COM classes
TLBEXP exports .NET types
Most users will have a seamless experience
AP 08/01
Calling into a COM component
Create .NET assembly from COM component via tlbimp
Client apps may access the newly created assembly
using System;
using System.Runtime.InteropServices;
using CONVERTERLib;
class Convert {
public static void Main(string [] args) {
CFConvert conv = new CFConvert();
...
fahrenheit = conv.CelsiusToFahrenheit( celsius );
AP 08/01
COM Support
Sometimes you need more control
Methods with complicated structures
as arguments
Large TLB only using a few classes
System.Runtime.InteropServices
COM object identification
Parameter and return value marshalling
HRESULT behavior
AP 08/01
COM Support
Example
[Guid(56A868B1-0AD4-11CE-B03A-0020AF0BA770)]
interface IMediaControl
{
void Run();
void Pause();
void Stop();
void RenderFile(string strFilename);
}
Back to projects
AP 08/01
The Evolution of Components
(Microsoft-style)
Fine-grain Integration
CLR
Course-grain Integration
XML
HTTP
Win32
C++
VB6
COM
DCOM
AP 08/01
Type fragmentation under COM
Lib1
COM Type System
Lib2
Lib3
Internal Type System
AP 08/01
Pervasive Type in the CLR
Lib1
Lib2
Lib3
CLR Type System
AP 08/01
Type segregation under XML
Lib1
XML Type System
Lib2
Lib3
Internal Type System
AP 08/01
Object Creation and the Loader
Win32
COM
CLR
LoadLibrary
CoCreateInstance
Assembly.Load
GetProcAddress
QueryInterface
GetType
MZ Header
MZ Header
MZ Header
PE Header
PE Header
PE Header
COFF Header
COFF Header
COFF Header
Type Library
CLR Metadata
Executable
Machine
Code
Executable
IL/Machine
Code
Executable
Machine
Code
AP 08/01
CLR Architecture
Your Managed
Code
MSCOREE.DLL
"The Runtime"
MSCORLIB.DLL
"The Runtime
Library"
MZ Header
MZ Header
MZ Header
PE Header
PE Header
PE Header
COFF Header
COFF Header
COFF Header
CLR Metadata
Type Library
CLR Metadata
IL/Machine Code
Machine Code
IL/Machine code
AP 08/01
on-the-fly COM
Obj1
CCW
RCW
mscoree.dll
Obj2
~mscoree.dll
COM callable wrapper (CCW)
runtime callable wrapper (RCW)
AP 08/01
The CLR Type System
T[]
Decimal
SByte
Array
Byte
Int16
String
UInt16
Int32
Object
ValueType
UInt32
Int64
interface T
UInt64
class T
Single
Boolean
Delegate
Double
Char
delegate T
struct T
Enum
enum T
AP 08/01
Metaprogramming and Reflection
in .NET
Remote method
invocation
COM Type
Information
JVM Type
Information
Transparent
Proxies and
Interception
Directory
Type
Information
CLR Type
Information
Automatic
Serialization
Code
Generation
DBMS Type
Information
XML Type
Information
Documentation
and Analysis
AP 08/01
Pervasive type and GetType
Type
Patient
typeof(Patient)
this.id.GetType()
Type
Int32
typeof(int)
this.age.GetType()
Type
Double
typeof(double)
this.dead.GetType()
Type
Boolean
typeof(bool)
this.GetType()
id
age
dead
class Patient
{
int id;
double age;
bool dead;
}
AP 08/01
Pervasive type and System.Type
typeof(Patient)
Type
Patient
GetType()
typeof(int)
Type
Int32
GetType()
Type
Type
typeof(double)
Type
Double
GetType()
GetType()
typeof(Type)
typeof(bool)
Type
Boolean
GetType()
Back to overview
AP 08/01