text
stringlengths
0
13.4k
}
List<Customer> customers = new List<Customer>();
customers.Add(new Customer());
customers.Add(new Customer());
customers.ForEach(c => c.SendEmail());
GoalKicker.com – .NET Framework Notes for Professionals
46
Chapter 10: Reflection
Section 10.1: What is an Assembly?
Assemblies are the building block of any Common Language Runtime (CLR) application. Every type you define,
together with its methods, properties and their bytecode, is compiled and packaged inside an Assembly.
using System.Reflection;
Assembly assembly = this.GetType().Assembly;
Assemblies are self-documenting: they do not only contain types, methods and their IL code, but also the Metadata
necessary to inspect and consume them, both at compile and runtime:
Assembly assembly = Assembly.GetExecutingAssembly();
foreach (var type in assembly.GetTypes())
{
Console.WriteLine(type.FullName);
}
Assemblies have names which describes their full, unique identity:
Console.WriteLine(typeof(int).Assembly.FullName);
// Will print: "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
If this name includes a PublicKeyToken, it is called a strong name. Strong-naming an assembly is the process of
creating a signature by using the private key that corresponds to the public key distributed with the assembly. This
signature is added to the Assembly manifest, which contains the names and hashes of all the files that make up the
assembly, and its PublicKeyToken becomes part of the name. Assemblies that have the same strong name should
be identical; strong names are used in versioning and to prevent assembly conflicts.
Section 10.2: Compare two objects with reflection
public class Equatable
{
public string field1;
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
var type = obj.GetType();
if (GetType() != type)
return false;
var fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic |
BindingFlags.Public);
foreach (var field in fields)
if (field.GetValue(this) != field.GetValue(obj))
return false;
return true;
}
GoalKicker.com – .NET Framework Notes for Professionals
47
public override int GetHashCode()
{
var accumulator = 0;
var fields = GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic |
BindingFlags.Public);
foreach (var field in fields)
accumulator = unchecked ((accumulator * 937) ^ field.GetValue(this).GetHashCode());
return accumulator;
}
}
Note: this example do a field based comparasion (ignore static fields and properties) for simplicity
Section 10.3: Creating Object and setting properties using
reflection
Lets say we have a class Classy that has property Propertua
public class Classy
{