Generics support in SLPS? (FAQ127)

Can I protect methods using generic code constructs?  Can I protect generic methods or methods on a generic class?

As of version 3.1.1915.38 of Code Protector it is now possible to protect methods using generic code constructs. As with previous releases of Code Protector, it is not yet possible to protect generic methods or methods on a generic class; this capability will be delivered in a future release.

[Note: An explanation of the technical terms used within this document can be found at http://msdn.microsoft.com/en-us/library/aa479859.aspx]

So you may now use the following generic constructs in protected code:

  • Declaring variables of closed constructed generic types, such as List<string>, List<List<int>> etc.
  • Calling methods on closed constructed generic types
	 class GenericClass<T>
	 {
		void M(T a){}   // parameter of generic class argument type
		void N(List<T> a){}  // parameter of constructed generic type using generic class argument type
	 }

	 //inside a protected method...
	 var a = new GenericClass<string>();
	 a.M("Hello World!"); // allowed
  • Calling closed generic methods, i.e. methods whose type arguments do not contain any generic parameters
	class NormalClass
	{
		void M<T>(T a){} // parameter of generic method argument type
		void N<T>(List<T> a){} // parameter of constructed generic type using generic method argument type
	}

	 //inside a protected method...
	 var a = new NormalClass();
	 a.M("Hello World!"); // allowed
	 a.N(new List<string>(){"Hello", "World", "!"}; // allowed, type inferred
  • Calling generic methods that have overloads declared on the same type with the same number of generic type parameters and method parameters e.g.
	class NormalClass
	{
		void M<T>(Func<T> a){}
		void M<T>(List<T> a){}
	}

	//inside protected method...
	var a = new NormalClass();
	a.M(() => "Hello World!")// allowed
	a.N(new List<string>(){"Hello", "World", "!"}; // allowed

 

Link to this

FAQ127

You can link to this item directly, using the shorter URL support.inishtech.com/FAQ127.