Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

Tagged as

Go to top

Lazy Alternatives - LazyAndWeak and BackgroundLoader

, 1 Dec 2011
This article will present two alternatives to Lazy.
LazyAlternatives.zip
LazyAlternatives
LazyAlternatives.suo
LazyAlternativesSample
Properties
Settings.settings
Pfz
Caching
Collections
DataTypes
DynamicObjects
Internal
Extensions
Factoring
Pfz.csproj.user
Pfz.Phone.csproj.user
Pfz.ruleset
Pfz.Silverlight.csproj.user
Pfz.snk
Pfz.suo
PhoneSpecific
Properties
Remoting
Instructions
Internal
Serializers
Udp
Serialization
BinaryBuiltIn
Threading
Contexts
Disposers
Unsafe
using System;
using System.Reflection;
using System.Reflection.Emit;

namespace Pfz.Extensions
{
	/// <summary>
	/// Adds some methods to emit code more safely.
	/// </summary>
	public static class PfzEmitExtensions
	{
		private static readonly MethodInfo _getTypeFromHandle = ReflectionHelper.GetMethod(() => Type.GetTypeFromHandle(new RuntimeTypeHandle()));
		private static readonly MethodInfo _getFieldFromHandle = ReflectionHelper.GetMethod(() => FieldInfo.GetFieldFromHandle(new RuntimeFieldHandle()));
		private static readonly MethodInfo _getProperty = ReflectionHelper.GetMethod(() => typeof(string).GetProperty("Length"));

		/// <summary>
		/// Emits a Ldnull.
		/// </summary>
		public static void EmitLoadNull(this ILGenerator generator)
		{
			if (generator == null)
				throw new ArgumentNullException("generator");

			generator.Emit(OpCodes.Ldnull);
		}

		/// <summary>
		/// Emits a Ldloc, receiving a localBuilder.
		/// </summary>
		public static void EmitLoadLocal(this ILGenerator generator, LocalBuilder localBuilder)
		{
			if (generator == null)
				throw new ArgumentNullException("generator");

			generator.Emit(OpCodes.Ldloc, localBuilder);
		}

		/// <summary>
		/// Emits a Stloc, receiving a localBuilder.
		/// </summary>
		public static void EmitStoreLocal(this ILGenerator generator, LocalBuilder localBuilder)
		{
			if (generator == null)
				throw new ArgumentNullException("generator");

			generator.Emit(OpCodes.Stloc, localBuilder);
		}

		/// <summary>
		/// Emits a Ldc_I4, receiving the value as parameter.
		/// </summary>
		public static void EmitLoadInt32(this ILGenerator generator, int value)
		{
			if (generator == null)
				throw new ArgumentNullException("generator");

			generator.Emit(OpCodes.Ldc_I4, value);
		}

		/// <summary>
		/// Emits Ldarg, receiving the index.
		/// </summary>
		public static void EmitLoadArgument(this ILGenerator generator, int argumentIndex)
		{
			if (generator == null)
				throw new ArgumentNullException("generator");

			generator.Emit(OpCodes.Ldarg, argumentIndex);
		}

		/// <summary>
		/// Emits a Newobj. You must give a valid constructor.
		/// </summary>
		public static void EmitNewObject(this ILGenerator generator, ConstructorInfo constructor)
		{
			if (generator == null)
				throw new ArgumentNullException("generator");

			generator.Emit(OpCodes.Newobj, constructor);
		}

		/// <summary>
		/// Emits a Newarr. You must supply the elementType.
		/// So, for int[], pass the typeof(int).
		/// </summary>
		public static void EmitNewArray(this ILGenerator generator, Type elementType, int count)
		{
			if (generator == null)
				throw new ArgumentNullException("generator");

			generator.EmitLoadInt32(count);
			generator.Emit(OpCodes.Newarr, elementType);
		}

		/// <summary>
		/// Emits a Ldfld, and receives the FieldInfo of the field to load.
		/// </summary>
		public static void EmitLoadField(this ILGenerator generator, FieldInfo field)
		{
			if (generator == null)
				throw new ArgumentNullException("generator");

			generator.Emit(OpCodes.Ldfld, field);
		}

		/// <summary>
		/// Emits a Ldsfld, and receives the FieldInfo of the static field to load.
		/// </summary>
		public static void EmitLoadStaticField(this ILGenerator generator, FieldInfo field)
		{
			if (generator == null)
				throw new ArgumentNullException("generator");

			generator.Emit(OpCodes.Ldsfld, field);
		}

		/// <summary>
		/// Emits Stfld, and received the FieldInfo of the field to store.
		/// </summary>
		public static void EmitStoreField(this ILGenerator generator, FieldInfo field)
		{
			if (generator == null)
				throw new ArgumentNullException("generator");

			generator.Emit(OpCodes.Stfld, field);
		}

		/// <summary>
		/// Emits Stfld, and received the FieldInfo of the static field to store.
		/// </summary>
		public static void EmitStoreStaticField(this ILGenerator generator, FieldInfo field)
		{
			if (generator == null)
				throw new ArgumentNullException("generator");

			generator.Emit(OpCodes.Stsfld, field);
		}

		/// <summary>
		/// Emits Ret.
		/// </summary>
		public static void EmitReturn(this ILGenerator generator)
		{
			if (generator == null)
				throw new ArgumentNullException("generator");

			generator.Emit(OpCodes.Ret);
		}

		/// <summary>
		/// Emits Ldstr. You must provide a valid string to it.
		/// Null is replaces by LoadNull.
		/// </summary>
		public static void EmitLoadString(this ILGenerator generator, string value)
		{
			if (generator == null)
				throw new ArgumentNullException("generator");

			if (value == null)
				generator.EmitLoadNull();
			else
				generator.Emit(OpCodes.Ldstr, value);
		}

		/// <summary>
		/// Loads a field token and calls GetFieldFromHandle.
		/// </summary>
		public static void FullLoadToken(this ILGenerator generator, FieldInfo fieldInfo)
		{
			if (generator == null)
				throw new ArgumentNullException("generator");

			if (fieldInfo == null)
			{
				generator.Emit(OpCodes.Ldnull);
				return;
			}

			generator.Emit(OpCodes.Ldtoken, fieldInfo);
			generator.Emit(OpCodes.Call, _getFieldFromHandle);
		}

		/// <summary>
		/// Loads a Type token and calls GetTypeFromHandle.
		/// </summary>
		public static void FullLoadToken(this ILGenerator generator, Type type)
		{
			if (generator == null)
				throw new ArgumentNullException("generator");

			if (type == null)
			{
				generator.Emit(OpCodes.Ldnull);
				return;
			}

			generator.Emit(OpCodes.Ldtoken, type);
			generator.Emit(OpCodes.Call, _getTypeFromHandle);
		}

		/// <summary>
		/// Pushes the PropertyInfo to the stack.
		/// </summary>
		public static void FullLoadToken(this ILGenerator generator, PropertyInfo property)
		{
			if (generator == null)
				throw new ArgumentNullException("generator");

			if (property == null)
			{
				generator.Emit(OpCodes.Ldnull);
				return;
			}

			generator.FullLoadToken(property.DeclaringType);
			generator.EmitLoadString(property.Name);
			generator.Emit(OpCodes.Callvirt, _getProperty);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Paulo Zemek
Architect
Canada Canada
I started to program computers when I was 11 years old, as a hobbist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.
 
At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do they work easier, faster and with less errors.
 
Want more info or simply want to contact me?
Take a look at: http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com
 
Codeproject MVP 2012
Microsoft MVP 2013

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 1 Dec 2011
Article Copyright 2011 by Paulo Zemek
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid