December 21, 2006

C++/CLI Quick Comparison with C#

The following table provides a summary of the most common constructs for quick reference.
Description C++/CLI C#
Allocate reference type ReferenceType^ h = gcnew ReferenceType; ReferenceType h = new ReferenceType();
Null reference ReferenceType^ h = nullptr; ReferenceType h = null;
Allocate value type ValueType v(3, 4); ValueType v = new ValueType(3, 4);
Reference type, stack semantics ReferenceType h; N/A
Calling Dispose method ReferenceType^ h = gcnew ReferenceType;

delete h;

ReferenceType h = new ReferenceType();


Implementing Dispose method ~TypeName() {} void IDisposable.Dispose() {}
Implementing Finalize method !TypeName() {} ~TypeName() {}
Boxing int^ h = 123; object h = 123;
Unboxing int^ hi = 123;

int c = *hi;

object h = 123;

int i = (int) h;

Reference type definition ref class ReferenceType {};

ref struct ReferenceType {};

class ReferenceType {}
Value type definition value class ValueType {};

value struct ValueType {};

struct ValueType {}
Using properties h.Prop = 123;

int v = h.Prop;

h.Prop = 123;

int v = h.Prop;

Property definition property String^ Name
String^ get()
return m_value;
void set(String^ value)
m_value = value;
string Name
return m_name;
m_name = value;
Exposing an event TODO // expose the event
event System::EventHandler^ OnEventOccurrance;

//Fire the event
if (OnEventOccurrance != nullptr)
OnEventOccurrance(this, gcnew System::EventArgs());
Simple type methods, double example double.IsNaN(someDouble); double.NaN; System::Double::IsNaN(someDouble)

December 1, 2006

Authentication and Authorisation in .NET

Detailed workshop on Principal Based security - Simple samples. Authentication is the part of verifying your identity. Authorization is determining whether or not a user has the permission to perform an action in the application. .NET framework provides access to the user through an identity and authorization access through a principal. Principal is (user or group of users) The framework provides two different types of principals, A Windows principal (WindowsPrincipal). It works against the underlying Windows OS A generic principal (GenericPrincipal). A principal and identity that is not bound to the underlying Windows user. + custom principal and identity by implementing the IPrincipal and IIdentity interface.

Reflection And Custom Attributes

Reasonable explanation of reflection Defining a custom class attribute
[AttributeUsage(AttributeTargets.Class)] // attribute is for classes only
public class CustomClassAttribute : System.Attribute
    private string m_Caption;
    public CustomClassAttribute(string caption)
       m_Caption = caption;

    public string Caption
       get { return m_Caption; }
Processing the Assembly to get the attributes
public void Discover()
    Type[] types = System.Reflection.
    foreach (Type type in types) // Go over all the types
      // Look for all types derived from BaseClass 
      if (type.BaseType == typeof(BaseClass)) 
        // Look for the 'CustomClassAttribute' attributes on that type
        object[] attributes = 
type.GetCustomAttributes(typeof(CustomClassAttribute), false);
        if (attributes.Length > 0)
          // iterate through the attributes, retrieving the 
          // properties
          foreach (Object attribute in attributes)
            CustomClassAttribute fca = (CustomClassAttribute)attribute;
            DoSomething(type.FullName, fca.Caption);
The attribute is used like this
[CustomClass("Some caption")]
public class SomeClass : BaseClass 

For example, say you have some properties on a type that you wish to iterate through but you do not wish to get the Obsolete ones (marked using "[Obsolete]", then the code would be something like this:
Type type = typeof(MyXXXType);
PropertyInfo[] props = type.GetProperties();
foreach (PropertyInfo prop in props)
    object[] attributes = prop.GetCustomAttributes(typeof(ObsoleteAttribute), false);
    if (attributes.Length > 0) // If property is marked as Obsolete then ignore it

    // otherwise do something with the property

Converting an Integer to a string in Managed C++

String* msg;
msg = String::Format(S"b{0:X4}", __box(m_intValue));
System::Int32 test = iExposureMS*1000; 
String* sst = String::Format("sst {0}", test.ToString());