Registry.LocalMachine

   

using System;
using Microsoft.Win32;
using System.Security.Permissions;

[RegistryPermissionAttribute(SecurityAction.Demand)]
class Class1 {
    static void Main(string[] args) {

        RegistryKey myRegKey = Registry.LocalMachine;
        myRegKey = myRegKey.OpenSubKey("SOFTWAREMicrosoftWindows NTCurrentVersion");

        Object oValue = myRegKey.GetValue("RegisteredOwner");
        Console.WriteLine("OS Registered Owner: {0}", oValue.ToString());

    }
}
           
          


new RegexCompilationInfo(@”^d{4}$”,RegexOptions.Compiled, “PinRegex”, “”, true)

   
 

using System;
using System.Reflection;
using System.Text.RegularExpressions;

    class MainClass
    {
        public static void Main()
        {
            RegexCompilationInfo[] regexInfo = new RegexCompilationInfo[2];
            regexInfo[0] = new RegexCompilationInfo(@"^d{4}$",RegexOptions.Compiled, "PinRegex", "", true);
            regexInfo[1] = new RegexCompilationInfo(@"^d{4}-?d{4}-?d{4}-?d{4}$",RegexOptions.Compiled, "CreditCardRegex", "", true);
            AssemblyName assembly = new AssemblyName(); 
            assembly.Name = "MyRegEx";

            Regex.CompileToAssembly(regexInfo, assembly);
       }
 }

    


Regex.CompileToAssembly

   
 

using System;
using System.Reflection;
using System.Text.RegularExpressions;

class MainClass {
    public static void Main() {
        RegexCompilationInfo[] regexInfo = new RegexCompilationInfo[2];
        regexInfo[0] = new RegexCompilationInfo(@"^d{4}$", RegexOptions.Compiled, "PinRegex", "", true);
        regexInfo[1] = new RegexCompilationInfo(@"^d{4}-?d{4}-?d{4}-?d{4}$", RegexOptions.Compiled, "CreditCardRegex", "", true);
        AssemblyName assembly = new AssemblyName();
        assembly.Name = "MyRegEx";

        Regex.CompileToAssembly(regexInfo, assembly);
    }
}

    


Uses an object in another application domain

   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example18_3.cs uses an object in another application domain
*/

using System;
using System.Runtime.Remoting;
using System.Reflection;

public class Example18_3 
{

  public static void Main() 
  {

    // create a new appdomain
    AppDomain d = AppDomain.CreateDomain("NewDomain");
    
    // load an instance of the System.Rand object
    ObjectHandle hobj = d.CreateInstance("Example18_2", "SimpleObject");
    // use a local variable to access the object
    SimpleObject so = (SimpleObject) hobj.Unwrap();
    Console.WriteLine(so.ToUpper("make this uppercase"));
  }

}


//=================================================

/*
  Example18_2.cs defines a simple object to create
*/

using System;

[Serializable]
public class SimpleObject 
{

  public String ToUpper(String inString)
  {
    return(inString.ToUpper());
  }

}



           
          


Illustrates unloading an application domain

   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example18_4.cs illustrates unloading an application domain
*/

using System;
using System.Runtime.Remoting;
using System.Reflection;

public class Example18_4 
{

  public static void Main() 
  {

    // create a new appdomain
    AppDomain d = AppDomain.CreateDomain("NewDomain");
    
    // load an instance of the SimpleObject class
    ObjectHandle hobj = d.CreateInstance("Example18_2", "SimpleObject");
    // use a local variable to access the object
    SimpleObject so = (SimpleObject) hobj.Unwrap();
    Console.WriteLine(so.ToUpper("make this uppercase"));

    // unload the application domain
    AppDomain.Unload(d);
    Console.WriteLine(so.ToUpper("make this uppercase"));

  }

}


//===================================================

/*
  Example18_2.cs defines a simple object to create
*/

using System;

[Serializable]
public class SimpleObject 
{

  public String ToUpper(String inString)
  {
    return(inString.ToUpper());
  }

}



           
          


Illustrates runtime type invocation

   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example17_6 illustrates runtime type invocation
*/

using System;
using System.Reflection;

public class Example17_6 
{

  public static void Main(string[] args) 
  {

    RandomSupplier rs;
    RandomMethod rm;

    // iterate over all command-line arguments
    foreach(string s in args)
    {
      Assembly a = Assembly.LoadFrom(s);

      // Look through all the types in the assembly
      foreach(Type t in a.GetTypes())
      {
        rs = (RandomSupplier) Attribute.GetCustomAttribute(
          t, typeof(RandomSupplier));
        if(rs != null)
        {
          // find the method in this class. assume that
          // the class only contains a single method.
          // can't use GetMethod() because we don't know
          // what the method is named
          foreach(MethodInfo m in t.GetMethods())
          {
            rm = (RandomMethod) Attribute.GetCustomAttribute(
             m, typeof(RandomMethod));
            if(rm != null)
            {
              // create an instance of the class
              Object o = Activator.CreateInstance(t);
              // create an empty arguments array
              Object[] aa = new Object[0];
              // invoke the method
              int i = (int) m.Invoke(o, aa);
              Console.WriteLine("Class {0} in {1} returned {2}",
                t, s, i);
            }

          }
        }
      }
    }

  }

}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example17_5a compiles into a library defining the RamdomSupplier attribute
  and the RandomMethod attribute
*/

using System;

// declare an attribute named RandomSupplier
[AttributeUsage(AttributeTargets.Class)]
public class RandomSupplier : Attribute
{
  public RandomSupplier()
  {
    // doesn't have to do anything
    // we just use this attribute to mark selected classes
  }
}

// declare an attribute named RandomMethod
[AttributeUsage(AttributeTargets.Method )]
public class RandomMethod : Attribute
{
  public RandomMethod()
  {
    // doesn't have to do anything
    // we just use this attribute to mark selected methods
  }
}
//===================================================
/*
  Example17_5b implements one class to supply random numbers
*/

// flag the class as a random supplier
[RandomSupplier]
public class OriginalRandom
{
  [RandomMethod]
  public int GetRandom()
  {
    return 5;
  }
}

//===================================================
/*
  Example17_5c implements one class to supply random numbers
*/

using System;

// flag the class as a random supplier
[RandomSupplier]
public class NewRandom
{
  [RandomMethod]
  public int ImprovedRandom()
  {
    Random r = new Random();
    return r.Next(1, 100);
  }
}

// this class has nothing to do with random numbers
public class AnotherClass
{
  public int NotRandom()
  {
    return 1;
  }
}
//===================================================
/*
  Example17_5d illustrates runtime type discovery
*/

using System;
using System.Reflection;

class Example17_5d 
{

  public static void Main(string[] args) 
  {

    RandomSupplier rs;
    RandomMethod rm;

    // iterate over all command-line arguments
    foreach(string s in args)
    {
      Assembly a = Assembly.LoadFrom(s);

      // Look through all the types in the assembly
      foreach(Type t in a.GetTypes())
      {
        rs = (RandomSupplier) Attribute.GetCustomAttribute(
         t, typeof(RandomSupplier));
        if(rs != null)
        {
          Console.WriteLine("Found RandomSupplier class {0} in {1}",
           t, s);
          foreach(MethodInfo m in t.GetMethods())
          {
            rm = (RandomMethod) Attribute.GetCustomAttribute(
             m, typeof(RandomMethod));
            if(rm != null)
            {
              Console.WriteLine("Found RandomMethod method {0}"
               , m.Name );
            }
          }        
        }
      }
    }

  }

}



           
          


Deeper Reflection: Invoking Functions

   

/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/

// 36 - Deeper into C#Deeper ReflectionInvoking Functions
// copyright 2000 Eric Gunnerson
// file=driver.cs
// compile with: csc driver.cs iprocess.cs
using System;
using System.Reflection;
using MamaSoft;

public class Deeper ReflectionInvokingFunctions
{
    public static void ProcessAssembly(string aname)
    {
        Console.WriteLine("Loading: {0}", aname);
        Assembly a = Assembly.LoadFrom (aname);
        
        // walk through each type in the assembly
        foreach (Type t in a.GetTypes())
        {
            // if it's a class, it might be one that we want.
            if (t.IsClass)
            {
                Console.WriteLine("  Found Class: {0}", t.FullName);
                
                // check to see if it implements IProcess
                if (t.GetInterface("IProcess") == null)
                continue;
                
                // it implements IProcess. Create an instance 
                // of the object.
                object o = Activator.CreateInstance(t);
                
                // create the parameter list, call it,
                // and print out the return value.
                Console.WriteLine("    Calling Process() on {0}", 
                t.FullName);
            object[] args = new object[] {55};
                object result;
                result = t.InvokeMember("Process",
                BindingFlags.Default |
                BindingFlags.InvokeMethod, 
                null, o, args);
                Console.WriteLine("    Result: {0}", result);
            }
        }
    }
    public static void Main(String[] args)
    {
        foreach (string arg in args)
        ProcessAssembly(arg);
    }
}


//=======================================================
// 36 - Deeper into C#Deeper ReflectionInvoking Functions
// copyright 2000 Eric Gunnerson
// file=IProcess.cs
namespace MamaSoft
{
    interface IProcess
    {
        string Process(int param);
    }
}

//=======================================================
// 36 - Deeper into C#Deeper ReflectionInvoking Functions
// copyright 2000 Eric Gunnerson
// file=process2.cs
// compile with: csc /target:library process2.cs iprocess.cs
using System;
namespace MamaSoft
{
    class Processor2: IProcess
    {
        Processor2() {}
        
        public string Process(int param)
        {
            Console.WriteLine("In Processor2.Process(): {0}", param);
            return("Shiver me timbers! ");
        }
    }
    class Unrelated
    {
    }
}

//========================================================
// 36 - Deeper into C#Deeper ReflectionInvoking Functions
// copyright 2000 Eric Gunnerson
// file=process1.cs
// compile with: csc /target:library process1.cs iprocess.cs
using System;
namespace MamaSoft
{
    class Processor1: IProcess
    {
        Processor1() {}
        
        public string Process(int param)
        {
            Console.WriteLine("In Processor1.Process(): {0}", param);
            return("Raise the mainsail! ");
        }
    }
}