text
stringlengths
0
13.4k
84
the same class
ConstructorBuilder intConstructor = tb.DefineConstructor(
MethodAttributes.Public, CallingConventions.Standard | CallingConventions.HasThis,
new[] { typeof(int) });
il = intConstructor.GetILGenerator();
il.Emit(OpCodes.Ldarg_0); // this
il.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[0])); // call parent's
constructor
il.Emit(OpCodes.Ldarg_0); // this
il.Emit(OpCodes.Ldarg_1); // our int argument
il.Emit(OpCodes.Stfld, fbNumber); // store argument in this.m_number
il.Emit(OpCodes.Ret);
var parameterlessConstructor = tb.DefineConstructor(
MethodAttributes.Public, CallingConventions.Standard | CallingConventions.HasThis, new
Type[0]);
il = parameterlessConstructor.GetILGenerator();
il.Emit(OpCodes.Ldarg_0); // this
il.Emit(OpCodes.Ldc_I4_S, (byte)42); // load 42 as an integer constant
il.Emit(OpCodes.Call, intConstructor); // call this(42)
il.Emit(OpCodes.Ret);
// And make sure the type is created
Type ourType = tb.CreateType();
// The types from the assembly can be used directly using reflection, or we can save the
assembly to use as a reference
object ourInstance = Activator.CreateInstance(ourType);
Console.WriteLine(ourType.GetProperty("Number").GetValue(ourInstance)); // 42
// Save the assembly for use elsewhere. This is very useful for debugging - you can use e.g.
ILSpy to look at the equivalent IL/C# code.
ab.Save(@"DynamicAssemblyExample.dll");
// Using newly created type
var myDynamicType = tb.CreateType();
var myDynamicTypeInstance = Activator.CreateInstance(myDynamicType);
Console.WriteLine(myDynamicTypeInstance.GetType()); // MyDynamicType
var numberField = myDynamicType.GetField("m_number", BindingFlags.NonPublic |
BindingFlags.Instance);
numberField.SetValue (myDynamicTypeInstance, 10);
Console.WriteLine(numberField.GetValue(myDynamicTypeInstance)); // 10
}
}
GoalKicker.com – .NET Framework Notes for Professionals
85
Chapter 24: .NET Core
.NET Core is a general purpose development platform maintained by Microsoft and the .NET community on GitHub.
It is cross-platform, supporting Windows, macOS and Linux, and can be used in device, cloud, and embedded/IoT
scenarios.
When you think of .NET Core the following should come to mind (flexible deployment, cross-platform, command-
line tools, open source).
Another great thing is that even if it's open source Microsoft is actively supporting it.
Section 24.1: Basic Console App
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("\nWhat is your name? ");
var name = Console.ReadLine();
var date = DateTime.Now;
Console.WriteLine("\nHello, {0}, on {1:d} at {1:t}", name, date);
Console.Write("\nPress any key to exit...");
Console.ReadKey(true);
}
}
GoalKicker.com – .NET Framework Notes for Professionals
86
Chapter 25: ADO.NET
ADO(ActiveX Data Objects).Net is a tool provided by Microsoft which provides access to data sources such as SQL
Server, Oracle, and XML through its components. .Net front-end applications can retrieve, create, and manipulate
data, once they are connected to a data source through ADO.Net with appropriate privileges.
ADO.Net provides a connection-less architecture. It is a secure approach to interact with a database, since, the
connection doesn't have to be maintained during the entire session.