Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Wednesday, 05 November 2014

As part of the c# there is a section on reflection:

Attributes eg

[Serializable] class Person { public string FirstName { get; set; } public string LastName { get; set; } }

asdf
[Fact] [Trait(“Category”, “Unit Test”)] public void MyUnitTest() { }

can derive from this:

public class CategoryAttribute : TraitAttribute { public CategoryAttribute(string value) : base(“Category”, value){ } } public class UnitTestAttribute : CategoryAttribute { public UnitTestAttribute() : base(“Unit Test”){ } }

then

[Fact] [UnitTest] public void MySecondUnitTest() {}

Interesting.

Reflection

Reflection enables an application to collection information about itself

public static void Main() { int i = 42; // GetType is a method on Object System.Type type = i.GetType(); // System.Int32 Console.WriteLine(type); }

If you want a plug-in system, and you drop in an assembly in a directory.  Need to use reflection.

class Program { public interface IPlugin { string Name { get; } string Description { get; } bool Load(MyApplication application); } public class MyApplication{} public static void Main() { Assembly pluginAssembly = Assembly.Load("testassembly"); var plugins = from type in pluginAssembly.GetTypes() //where typeof(IPlugin).IsAssignableFrom(type) && !type.IsInterface where !type.IsInterface select type; foreach (Type pluginType in plugins) { // Returns null instances IPlugin plugin = Activator.CreateInstance(pluginType) as IPlugin; } } }

and then the testassembly:
namespace testassembly { public interface IPlugin { string Name { get; } string Description { get; } bool Load(MyApplication application); } public class MyPlugin : IPlugin { public string Name { get { return "MyPlugin"; } } public string Description { get { return "My Sample Plugin"; } } public bool Load(MyApplication application) { return true; } } public class MyApplication { } }

Am not understanding this yet….
public static void Main() { Assembly myAssembly = Assembly.GetExecutingAssembly(); // Bring back classes Type[] myAssemblysTypes = myAssembly.GetTypes(); foreach (Type myType in myAssemblysTypes) { Console.WriteLine("myType Name: {0}", myType.Name); } // ConsoleAppliation2.exe Module[] myAssemblysModules = myAssembly.GetModules(); foreach (Module myModule in myAssemblysModules) { Console.WriteLine("myModule Name: {0}", myModule.Name); } // Assembly name and version var x = new MyApplication(); // mscorlib 4.0.0 // testassembly 1.0.0.0 AssemblyName[] referencedAssemblyNames = myAssembly.GetReferencedAssemblies(); foreach (AssemblyName assemblyName in referencedAssemblyNames) { Console.WriteLine("Assembly Name: {0}", assemblyName.Name); Console.WriteLine("Assembly Version: {0}", assemblyName.Version); } // Fields **not working Type t = typeof(Program); FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy); foreach (FieldInfo fi in fields) { Console.WriteLine("Field name {0}", fi.Name); Console.WriteLine("Field value {0}", fi.GetValue(null)); } }

not working yet :-)
var blah = new Blah(); blah.Age = 42; DumpObject(blah); } static void DumpObject(object obj) { FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); foreach (FieldInfo field in fields) { if (field.FieldType == typeof(int)) { Console.WriteLine(field.GetValue(obj)); } } } } public class Blah { public int Age { get; set; } private int _age; public Blah() { _age = 41; } }

Code that works, showing how to get values or public and private fields using reflection.

// Run a method on Blah MethodInfo doSomethingMethod = blah.GetType().GetMethod("DoSomething", new Type[] { typeof(int) }); int result = (int)doSomethingMethod.Invoke(blah, new object[] { 42 }); Console.WriteLine(result);

and the method is

public int DoSomething(int x) { return x + 1; }

CodeDom and lambdas to generate code

CodeDom to create an object graph that can be converted to a source file/binary assembly that can be executed.

public static void Main() { var compileUnit = new CodeCompileUnit(); var myNamespace = new CodeNamespace("MyNamespace"); myNamespace.Imports.Add(new CodeNamespaceImport("System")); var myClass = new CodeTypeDeclaration("MyClass"); var start = new CodeEntryPointMethod(); var cs1 = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression("Console"), "WriteLine", new CodePrimitiveExpression("Hello World!")); compileUnit.Namespaces.Add(myNamespace); myNamespace.Types.Add(myClass); myClass.Members.Add(start); start.Statements.Add(cs1); var provider = new CSharpCodeProvider(); using (var sw = new StreamWriter("HelloWorld.cs", false)) { var tw = new IndentedTextWriter(sw, " "); provider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions()); tw.Close(); } }

which generated:

//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // Runtime Version:4.0.30319.34014 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace MyNamespace { using System; public class MyClass { public static void Main() { Console.WriteLine("Hello World!"); } } }

wow!

Lambda functions were introduced in C#3.  It is a compact method to create an anonymous method.

// Func type takes 2 ints and returns and int. // Lambda - anonymous method Func<int, int, int> addFunc = (x, y) => x + y; Console.WriteLine(addFunc(2, 3));

Expression Trees

// The expression is first constructed with a call to Console.Write and Console.WriteLine. BlockExpression blockExpr = Expression.Block( Expression.Call(null,typeof(Console).GetMethod("Write", new Type[] { typeof(String) }),Expression.Constant("Hello ")), Expression.Call(null,typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),Expression.Constant("World!")) ); // Expression is compiled to an Action (as no return) Expression.Lambda<Action>(blockExpr).Compile()();

asdf
| | #