Saturday, April 12, 2014

Elegant sense of .NET Type Objects: Generics

I have always enjoyed the powers of reflection of .NET. Writing a small Search Functionality to work with Entity Framework as I explain in this blog post, spurred a lot of thinking and exploration.

Now I want to organize and make sense of the various properties that exist on a Type object, mostly the Boolean properties that describe several ‘characteristics’ of a .NET type:

I overview only the properties that pertain to generics, but I have created an interesting Excel sheet from which one can learn a few things, like typeof(int) is not a class, laid out sequentially and is sealed. You can find the sheet below.

1.       IsGenericType
This property indicates whether the type is generic or not, even if it is closed. For example
typeof(List).IsGenericType is true

2.       ContainsGenericParameters
This property indicates whether the type has generic parameters at any level of the nesting. For example the Type object typeof(List<>) will return true for this property. For a nested generic type example:
Type t = typeof(List<>);
Type t2 = t.MakeGenericType(t);
Console.WriteLine(t2.ContainsGenericParameters); //true

The truth of this property means that you can create an instance of such a type, because it is partially open (has some generic slots somewhere for which a type hasn’t been specified, see ).

3.       IsGenericParameter
This property indicates whether this Type represents a type that is used as a placeholder for another type that would be provided for a generic type to make it concrete. You can only get such types using the GetGenericTypeArguments() methods on a generic type that has not been made concrete. Such a type would have as true all the following properties: IsGenericType, IsGenericTypeDefinition and ContainsGenericParameters. So having all three as true is equivalent to GetGenericArguments().Length > 0.

Generic Parameters have attributes associated with them as well as constraints. Get these using GenericParameterAttributes and GetGenericParameterConstraints respectively, calling these methods on methods for which IsGenericParameter is false would cause an exception to be thrown.

GenericParameterAttributes is a flag based enumeration that represents the kind of attributes associated with the generic parameter such as the availability of a default constructor and being a reference type (class constraint). You can check for the various cases (and combinations thereof) using a switch with bitwise operators, like here:

public class SomeClass
{
            public static T MethodWithGenericParameterAttributes()
        where T : class
    {
        return null;
    }
}
 
 
class Program
{
    public static void Main(string[] args)
    {
        var genericMethod = typeof(SomeClass).GetMethod("MethodWithGenericParameterAttributes");
        var T = genericMethod.GetGenericArguments()[0];
        var ggpc = T.GetGenericParameterConstraints();
        var gpa = T.GenericParameterAttributes;
 
        switch (T.GenericParameterAttributes)
        {
            case GenericParameterAttributes.Contravariant:
                break;
            case GenericParameterAttributes.Covariant:
                break;
            case GenericParameterAttributes.DefaultConstructorConstraint | GenericParameterAttributes.ReferenceTypeConstraint:
                //reference type with a default constructor
                break;
            case GenericParameterAttributes.None:
                //no attributes
                break;
            case GenericParameterAttributes.NotNullableValueTypeConstraint:
                //value type
                break;
            case GenericParameterAttributes.ReferenceTypeConstraint:
                break;
            case GenericParameterAttributes.SpecialConstraintMask:
                break;
            case GenericParameterAttributes.VarianceMask:
                break;
            default:
                break;
        }
    }
}

4.       IsGenericTypeDefinition
This property indicates whether this type represents a definition of generic type, from which another Type can be constructed. You can construct concrete types from Generic Type Definitions, or partially closed types, as follows:
·         Creating Concrete types from generic type definitions: happens when passing a concrete type to MakeGenericMethod of a generic type that is a generic type definition.
·         Creating Partially closed types from generic type definitions: happens when passing a generic type that contains generic parameters to a generic type as an argument. This fills the generic parameter for the parent type, but still it will contain generic parameters. Here’s a code sample:


Type genericTypeDefinition = typeof(List<>);
Type partiallyClosedType = genericTypeDefinition.MakeGenericType(typeof(Dictionary<,>));

partiallyClosedType.ContainsGenericParameters will be true, so you can’t create instances from this type. partiallyClosedType.IsGenericTypeDefinition is false, so you can’t make a concrete type from partiallyClosedType. So is it useless? I think not.

Thursday, April 10, 2014

Sorry, you don't have 2c63df2b-ceab-42c6-aeff-b3968162d4b1.

Developer Executive Summary: You can deploy only SharePoint 2010 workflows to SharePoint Foundation 2013.

And it took me two intense days of experimentation to realize this. SharePoint 2013 Workflows, the beautiful declarative workflows with the XAML designer, do not work with SharePoint Foundation 2013 and only work with SharePoint Server 2013.

2c63df2b-ceab-42c6-aeff-b3968162d4b1 is the feature Guid for WorkflowServiceStore feature existing in SharePoint Server 2013 and not SharePoint Foundation 2013.

I almost got depressed trying to understand why the call to *** which was working perfectly fine with SharePoint online, now returns null with SharePoint Foundation 2013 installed on-premise for testing. I wish it just threw an exception telling me that some feature is missing because it's a SharePoint Server Feature, but I just got null, and started to wonder whether there are connectivity problems, installation problems, deployment problems or permissions problems and so on!


WorkflowServicesManager hwsm = new WorkflowServicesManager(hostClientContext, hostClientContext.Web);
var hwds = hwsm.GetWorkflowDeploymentService();

hwds.ServerObjectIsNull is true after loading the object using the context!!! WHY?! I have permissions, everything is fine, the same exact code was working with SharePoint online? Why isn't it working with SharePoint 2013 on premise?! Because it's Foundation, and you don't have 2c63df2b-ceab-42c6-aeff-b3968162d4b1. Sorry, and sorry for not telling you that.

Here's a useful list of Features and their Guids that are available in SharePoint Server 2013 in a TechNet wiki article: http://social.technet.microsoft.com/wiki/contents/articles/14423.sharepoint-2013-existing-features-guid.aspx

Reflection Adventures in the Land of Linq to Entities

To save your valuable time, my dear fellow developer: this post is about Reflection, not Entity Framework or Linq.

Introduction:
I was writing a method to support dynamic search using Entity Framework. The purpose was to support search across all properties in any entity and context. So the method would take a list of properties to search in, and the query to search for (as this is obtained form the UI layer, it's usually just a string, but the method accepts objects which matters when generating Entity Framework generates SQL from Linq-to-Entities calls).

Attribute Adventures:
I didn't want to write search for each entity separately, so I was aiming to build search functionality that would accept any kind of object and property. I created a SearchableAttribute attribute that I would use to tag entity properties that I want to be selected for search (taking in consideration TypeMetadataAttribute), but that's another simple though interesting reflection-related topic in which I select which properties to display to the UI layer by searching for those properties with the attribute. I can also extend such functionality to link search more than one property if a particular property is being searched, using for example, a LinkedPropertyAttribute attribute that takes another property's name as a parameter. That's also just another simple property-finding reflection related issue, so let's get back to EF and finding generic methods using reflection:

The method would construct a Linq Expression dynamically, and use that while invoking the Where extension method of System.Linq.IEnumerable. The expressions constructed were an invocation to method 'Equals' if the queried property is not text, and invoked method 'Contains' if it is, they are all adjuncted using an OR binary expression. Entity Framework translates Contains to a LIKE clause using wildcards. This is all to have the ORM generate the proper SQL, and not to search on fetched data which is inefficient!

Side note: Now I wonder if Entity Framework supports custom translators that I can use to support my own methods? I got numerous exceptions related to lack of support to ToString() and other custom methods before. Maybe it's related to converting lambdas to expression trees, and then processing expression trees to get SQL as expression trees are essentially manipulable and queryable data that represent logic! How cool is that!

The Interesting Generics:
One interesting part was trying to find the Where method from the System.Linq.IEnumerable static class, to invoke it on a reference that is declared as object. My bad design did not have a base class for the POCO entities, but my plan is to change that when more need arises. But the catch is that I would have needed to do things in the same way even if I had a base class as Linq Expressions need to work on derived objects and types that actually have the properties being searched. (I noticed this when I found out that Linq automatically infers the return type of a property access when you create an expression for that and won't work if you supply a property that does not exist in the type, as I explain here **TO BE DONE**).

The primary question in finding the Where method is how to use reflection to find generic methods. If you have a Type object, you can simply use GetMethod(string name, params Type[]) to exactly specify the method to be retrieved, but in case of generic parameters it's trickier because even a type constructed from the generic method parameter type won't match the generic parameter type from which it was constructed. See the following example:

public static class Test
{
    public static T Example1(T i)
    {
        return i;
    }
 
    public static List Example2(List i)
    {
        return i;
    }
}
 
public static class Test2
{
    public static T Example1(T i)
    {
        return i;
    }
 
    public static List Example2(List i)
    {
        return i;
    }
 
    public static int Example1(string i)
    {
        return -9 * DateTime.Now.Minute;
    }
 
}
 
 
class Program
{
    public static void Main(string[] args)
    {
        Type testType = typeof(Test);
        //successfully returns Example1 MethodInfo without specifying 
        //type arguments, only because we have one method with that name.
        MethodInfo example1 = testType.GetMethod("Example1");
 
        Type test2Type = typeof(Test2);
        MethodInfo example1_again = null;
        //fails because of ambiguous match
        try
        {
            example1_again = test2Type.GetMethod("Example1");
        }
        catch (Exception x)
        {
            Console.WriteLine(x.Message);
        }
 
        //okay let's try again
        if (example1_again == null)
        {
            Console.WriteLine("We could not get Example1 method from Test2 type by name because of multiple matches.");
            //searching by parameter type.
            //parameter type is List, let's create Type object for List?
            var listType = typeof(List<>);
            example1_again = test2Type.GetMethod("Example1"new Type[] { listType });
        }
 
        //okay, never give up.
        if (example1_again == null)
        {
            Console.WriteLine("We could not get Example1 method from Test2 type by name and typeof(List<>).");
            var listType = typeof(List<>).MakeGenericType(typeof(int)); //List
            example1_again = test2Type.GetMethod("Example1"new Type[] { listType });
        }
 
        //what could possiblity match?! Okay, let's try getting the type of List, and then supply THAT to GetMethod!
        if (example1_again == null)
        {
            Console.WriteLine("We could not get Example1 method from Test2 type by name and typeof(List).");
            MethodInfo example2Method = typeof(Test).GetMethod("Example2");
            var listType = example2Method.GetParameters()[0].ParameterType;
            //ListType is now a very curious type: listType.GetGenericArguments()[0].Name
            //returns T. Interestingly, this is still not a GenericTypeDefinition and we can't use
            //this to match for the generic method. Funny: Identical generic type defs
            //don't match each other!
            example1_again = test2Type.GetMethod("Example1"new Type[] { listType });
 
            Console.WriteLine("listType.IsGenericTypeDefinition = " + listType.ContainsGenericParameters); //true! It contains T.
            Console.WriteLine("listType.IsGenericTypeDefinition = " + listType.IsGenericTypeDefinition); //false! It can't be made using MakeGenericType
            Console.WriteLine("listType.GetGenericArguments()[0].IsGenericParameter = " + listType.GetGenericArguments()[0].IsGenericParameter);
 
            //okay let's try again by getting the generic type definition!
            listType = listType.GetGenericTypeDefinition();
            example1_again = test2Type.GetMethod("Example1"new Type[] { listType });
        }
 
        if (example1_again == null)
        {
            Console.WriteLine("We could not get Example1 method from Test2 type by name and List type object obtained from a similar method's type argument, nor using a Generic Type Definition created from that type argument!");
            Console.WriteLine("Oh my cat! What to do? :(");
            Console.WriteLine("...my cat thinks for a second...");
            System.Threading.Thread.Sleep(1000);
            Console.WriteLine("Cat: 'Just search for it! This is what I do to find your expensive socks to tear them!'");
            Console.WriteLine("Okay cat, thank you (that is for the idea! not for tearing my socks!).");
        }
            
    }
}

But then, what could match? Read on.

What the cat ended up doing
Nothing that I could find. So our final resort is to search for the wanted method (just loop over the existing methods and check for parameter counts and types), like the cat told me.

So, I ended up essentially searching for the method in the System.Linq.IEnumerable type, instead of just using the seemingly simple to use Type.GetMethod. The primary issue is related to matching generic types. I still believe there should be a way, but it is worth exploring as to the relationship between partially closed (open) generic types and generic type definitions.

Generic Type Definitions?
To explain generic type definitions, and their parameters, consider the following example:

typeof(List<>) is a Type representing a generic type definition, the type argument for such a parameter is not null, but rather an actual Type object for a type called a 'generic type definition parameter type' named T. Not so tricky if you think about it: it's just a made-up type acting as a placeholder. What's tricky is making sense of the fact that T ContainsGenericParameters = true, and IsGenericTypeDefinition = false, just like the type of the parameter of the generic method Example2 in both test classes above.

Sunday, April 6, 2014

SharePoint! Get-SPManagedAccount

I was working on a SharePoint PowerShell script on a new SharePoint 2013 test environment, when to my surprise, executing Get-SPManagedAccount "domain\testuser" threw an error: "Get-SPManagedAccount : No matching accounts were found."

I got even more surprised when I tried to execute Get-SPManagedAccount without any parameters and got no results, as that supposed to list all of the managed accounts. I started to think in the direction of active directory (note the domain\ prefix in the account I'm trying to use) but quickly realized that this is the incorrect direction: managed accounts are "SharePoint" managed accounts, not Active Directory ones, meaning they need to be added using Central Administration to the Managed Accounts list.

Central Administration => Security => Configure managed accounts (under General Security).

Add an existing user using the Register Managed Account link (button).

So Managed Accounts are just any Windows account that I add to SharePoint so that it can be used by SharePoint components.

Tuesday, April 1, 2014

Windows Phone 8: a reference to a higher version or incompatible assembly

The title of this post is exactly what I searched for when facing this error while trying to work with Smaato's (SOMA) advertisement in Windows Phone 8, except without the colon. I am tired, and even the simplest errors can get me down, especially when they are so mysterious.

Why would a Windows Phone 8 SDK be off a different version?!

The answer is that is NOT!

Turns out the message is so misleading, reminds me of the cryptic COM Exceptions of long ago! I realized that the only thing I need to do is to unblock the DLL by right-clicking the file, properties, then Unblock. It was blocked for security reasons.




Saturday, March 29, 2014

Back to Windows Phone 8: Wave Files Support Intricacies

Creating an XNA SoundEffect object simply requires a Stream object of WAV format sound data (whether from file or in memory).

Microsoft.Xna.Framework.Audio.SoundEffect sei = SoundEffect.FromStream(ms);

Make sure the Position of the Stream object is 0 (or wherever the actual beginning of the WAV file header is--not the data)!

But that's only part of the story: so after I discovered that one can't play MP3 or WMA using SoundEffect, I was hit by another mysterious exception, . That you can usually safely interpret as a problem decoding an unsupported file format.

Today I discovered that I can't even play 44.100 kHz, 4 Bit, Stereo or Mono Wav files, but I can play 22.050 kHz, 8 Bit, Mono ones. Which ones are supported? Which one's aren't?!

Thursday, March 27, 2014

Good Mistake to Discover: Microsoft.Ajax.Utilities.Expression

I was experimenting with Linq Expressions in a test Visual Studio solution when I finally got a method working and copy-pasted it in my main Solution, which is a Web Project.

Copy-paste, click on Expression, pressing Ctrl+. (Ctrl + dot), to add a using statement as where I was pasting I wasn't using System.Linq.Expressions, and then quickly press enter to choose the first option which was the incorrect one: Microsoft.Ajax.Utilities.Expression!

But what's that?! By my simple mistake I discovered that Microsoft actually built a JavaScript parser for developers that can generate JavaScript code dynamically. That is interesting especially after learning about the trend to create APIs and libraries that can be used to dynamically create JavaScript code using, JavaScript (or CoffeeScript for that matter). Checkout: Ariya Hadayat's Esprima on GitHub. It's a JavaScript library to programmatically manipulate JavaScript code constructs!

Now I'll explore more about both technologies, but I wonder if Microsoft created this after or before Ariya Hadayat's famous Esprima.