Welcome to MSDN Blogs Sign in | Join | Help
This post has been long overdue ... I have moved my blog to http://blogs.iis.net/nazim/ since I moved to the Internet Information Server team 2 years ago. I still get questions about NETCF from this blog, but I rarely get time to respond ... mostly because I don't know the answers on the top of my head anymore.

I have been reading much these days about reform at Microsoft, both technology and management-wise on all these different blog sites like mini msft. I have taken an analytic stance while reading a lot of these posts; not because I don't think they concern me, but mostly because I understand most of these perspectives and I am rarely shocked by any of them.

While doing this it struck me that perhaps it's time for me to do my own research ... a very basic one at that. I am going to ask random people I approach in everyday life "Name me one thing you really hate about any Microsoft OS". After the first 3 people asked me what an "OS" was I felt like these people must be from Mars or something and that I should perhaps ask a different sort of people.

But it came quite quickly to me that my sample was not flawed and that any attempt to change my sample was going to skew the results. After, the first three tries, it took me about a week to start asking again. So I rephrased my question the next time around to "Name me one thing you really hate about any Microsoft product".

I have asked this question to 63 people in 6weeks so far (yes I don't get out as often as I would like to), and to my surprise only 16 people named one thing, almost everyone else named more than one!! 7 people either brushed off my question with an "I don't care" look or didn't seem to notice me at all. Maybe they thought I was trying to sell them something :) Even more surprising, the 16 people that named only one item seemed to be on the more knowledgable side of technology (yeah I know this a'int exactly analytical ... but after being in the tech industry for 6+ years, my gut instinct has rarley failed me).

Frankly, this was baffling in itself ... but it does not end here. More than half said that MSN search "sucked" and that Google was so much better. The funny thing is that I didn't really have MSN search on my mind when I was talking about Microsoft products. Anyways, I humored these folks and asked them if they had tried something besides google and MSN search. Most answered "NO" instantaneously, however 3 of them asked me "What do you mean?".

The first one to ask me this was a clerk at a Seven Eleven ... and frankly he left me stumbling for words. "Have you tried using something like dogpile or AltaVista or anything like that?" I asked. "Never heard of them." said the clerk; the exact same response I got from the other two who asked me what I meant.

At least 30 people also told me that "Windows slows down my machine after running for a while" or variants thereof. 12 mentioned "Word doesn't do what I ask it do" or variants thereof.

The bottom line here is that I didn't hear IE or Vista or Visual Studio or things that most of these blogs seem to critically analyze even once. Not any in my sample seemed forward-looking. And yeah ... I live in Redmond, WA, so expected a lot of techies. I have tried to stay true to my sampling, but maybe I have unintentionally not approached anyone I thought would work for Microsoft. I am not going to attempt any explanation of the data, just present the data. I have some guesses, but I am sure I would keep second-guessing myself till I forget my own name.

So what do you folks think? Got any more questions for me to ask around? This would be a great way to build up my annoyance factor I guess. So if my career at Msft doesn't work out too well ... I could try selling insurance or vacuum cleaners to the aged :)

I spent some time playing with Whidbey and noted some AppDomain unload/termination behavior that is not entirely intuitive. These apply to both the full .NET framework as well as .NET Compact Framework 2.0.

The documentation resides at http://msdn2.microsoft.com/en-us/library/03yhawcy.

Here's a run-through of the scenarios where unload fails and an AppDomain tear-down occurs.

1) Thread blocked in native code.

Say that a thread in my AppDomain is blocked in a PInvoke call. Say Main() is running in AD1 and we create AD2 from here and the main thread in AD2 does a PInvoke that never returns. At this point if I try to unload AD2 from AD1, Thread.Abort on the blocked thread in AD2 doesn't return back and so the 10-second timeout kicks in. At this point AD2 is forcefully torn-down, we get a CannotUnloadAppDomainException in AD1. At this point if Main() in AD1 swallows this exception and returns, the application is still alive, because it still has a live thread in AD2.

2) Thread in a Constrained Execution Region (CER)

A CER is a region of code where

   a) The runtime is constrained from throwing out of band exceptions that would prevent the code from executing in its entirety.

   b) User code is constrained from executing code that would result in the throwing of out of band exceptions.

This includes ThreadAbortException handlers, finally clauses, finalizers etc. The AppDomain tear down occurs just like in "1)".

There are a whole bunch of disjoint samples and documentation out on the web right now for this task that is just cumbersome to use and integrate. So I thought I should integrate all these snippets (see links below for sources) into one uber solution (managed + native) for VS 2005 beta users. You can download this sample from

http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=5477acd6-d58d-41d0-9390-ed0daa1f9dda

What does this sample contain:

This is a VS 2005 project that contains both the native and managed portions in one easy to deply solution. The default configurations are to deploy to a Windows Mobile 5.0 Pocket PC. So besides VS 2005 Beta you will need the WM 5.0 SDK for Pocket PC and SmartPhone to use this project as intended.

It uses the receiving SMS message base sample from http://www.microsoft.com/downloads/details.aspx?FamilyId=98CCF3D4-DB7C-4A7D-A323-53BBDBBE0420&displaylang=en and tags along enhancements from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/ReceivingSMSMessages.asp and http://msdn.microsoft.com/mobility/default.aspx?pull=/library/en-us/dnnetcomp/html/receivingsmsmessages.asp

Disclaimer(s):
This posting is provided "AS IS" with no warranties, and confers no rights.
Some of the information contained within this post may be in relation to beta software. Any and all details are subject to change.

Well here is the Samsung SCH-S310 recently unveiled in Korea. What's special about this phone is the built-in 'Accelerometer' which can accurately calculate and ascertain movement in three dimensional space and then carry out commands according to those calculations. So you would write a '0' by moving the phone in that pattern and it will pick it up as such as an input.

It would be fascinating to see how the interfaces for phones evolve with the use of spatial recognition technology they employ in this new Samsung phone. You can check out the full story at BusinessWire.

1 Comments
Filed under:

Version 2 of .NET Compact Framework (NETCF) will provide support for parametric polymorphism aka Generics. You can get a good feel for the feature support from blog posts by Seth Demsey and Roman Batoukov. It would also be useful to go over Generics terminology as presented in this article in section 1.1.

 

Evidently since this language feature adds to the very nature of types in the CLR, one could safely assume need for special support when reflecting on these types … and such is the case. Below is a list of new Reflection APIs provided to assist in discovering information about a generic type/method through reflection. It should be noted however that previously existent reflection APIs like Type.GetMethods() etc would also discover generic methods and the like.

 

 

 

I. New Reflection APIs for generic support.

 

(Note: This list is current as of .NET Framework 2.0 Beta 1 and may change henceforth.)


 

1. System.Type

 

Properties

ContainsGenericParameters

HasGenericArguments

IsGenericParameter

IsGenericTypeDefinition

 

Methods

BindGenericParameters

GetGenericArguments

GetGenericTypeDefinition

 

 

2. System.Reflection.MethodInfo

 

Properties

ContainsGenericParameters

HasGenericArguments

IsGenericMethodDefinition

 

Methods

BindGenericParameters

GetGenericArguments

GetGenericMethodDefinition

 

 

 

II. How and where would I use it?


 

1. Creating generic types via reflection.

 

NETCF does not have runtime IL verification and metadata validation and hence cannot validate generic parameter constraints for normal execution scenarios (if the compiler did not catch the error at compile time). However, if one were to use reflection, i.e. get an open generic type and the bind parameters to it via BindGenericParameters() API, constraint validation would be performed. So if constraint validation is important to you, use the reflection API to bind parameters (if the compiler would not catch the error). Here is an example:

 

Say I have a generic type:

 

public class MyGenericType<T, U>

   where T: IFormattable

   where U: IConvertible

{}

 

Something like MyGenericType<Int32, Object> should not be allowed, since System.Object does not implement IConvertible. However if you were to write code that looked as below, we would validate the constraints on MyGenericType and throw an exception.

 

Type foo = Type.GetType("MyGenericType`2");

Type[] params = new Type[] {typeof(Int32), typeof(Object)};

Type bar = foo.BindGenericParameters(params); // This will throw an exception because constraints are not met.

 

Type foo above denotes the open type for MyGenericType, meaning no generic parameters have been bound to it yet. The open type is also referred to as the Generic Type Definition. The "`2" (back-tick) notation refers to the number of generic parameters on the type. So a class like

 

public class GenType2<T1, T2, T3, T4>

 

would have an open type notation of

 

Type foo2 = Type.GetType("GenType2`4");

 

Creating a bound type for GenType2 would look something like what is shown below. As one would guess, the types foo3, foo4 and foo5 are identical.

 

Type foo3 = Type.GetType("GenType2`4[System.Int32, System.Int32, System.Int32, System.Int32]");

GenType2<Int32, Int32, Int32, Int32> bar2 = new GenType2<Int32, Int32, Int32, Int32>();

Type foo4 = bar2.GetType();

Type foo5 = typeof(GenType2<Int32, Int32, Int32, Int32>);

 

What would the type string look like if one of the generic parameters above was a type in a different assembly? Well it would look something like what is shown below.

 

Type foo6 = Type.GetType("MyGenericClass`2[System.Int32, [MyNameSpace.MyExternType, MyExternAssemblyName]]");

 

Of course you could also go ahead and provide more information like the public key token if you wanted.

 

 

2. Invoking generic methods and methods with generic arguments using reflection

 

Say I have the following type definition.

 

public class MyGenericType <T, U>

{

   public void Method1 (T x) {}

   public void Method2<V> () {}

}

 

Method1 is a method which has a generic argument and Method2 would be a generic method. Hopefully this example helped untangling your brain from the topic being discussed. In Method1, the first argument x happens to be of the same type as the first generic parameter that MyGenericType was created with (T). In Method2 however, V is a generic type and has no relation to the generic parameters used to create MyGenericType. Here's how these methods would be called in the normal execution scenario.

 

MyGenericType<Int32, Double> mgt = new MyGenericType<Int32, Double>();

Int32 i = 0;

mgt.Method1( i );

mgt.Method2<System.DateTime>();

 

Here's how you would do it using reflection.

 

MethodInfo mi1 = typeof(mgt).GetMethod("Method1");

MethodInfo mi2 = typeof(mgt).GetMethod("Method2");

mi1.Invoke(mgt, new object[] {5}); // The method argument has to be castable to an Int32.

MethodInfo mi2_bound = mi2.BindGenericParameters(typeof(System.DateTime));

mi2_bound.Invoke(mgt, new object[] {});

MethodInfo mi2_unbound = mi2_bound.GetGenericMethodDefinition();

 

As you noticed we had to first bind a generic parameter to Method2 before we could Invoke it. In the code above, mi2_unbound would be the same as mi2.

 

 

 

III. Limitations


 

1. Getting members of open generic types

 

Getting members, methods or interfaces of open generic types is not supported in NETCF. You will get a NotSupportedException thrown when you attempt to do something similar to what is shown below.

 

Type t = Type.GetType("MyGenericType`2");

Type[] InterfaceList = t.GetInterfaces() // This will throw a NotSupportedException

MethodInfo mi = t.GetMethod("Method1"); // This will throw a NotSupportedException

 

What you would need to do to work around this is to simply bind some type parameters to your generic type (instantiate it) and then attempt to get its members.

 

Type t = Type.GetType("MyGenericType`2[System.Int32, System.Int32]");

MethodInfo mi = t.GetMethod("Method1"); // This will work.

 

 

2. Methods/Properties not supported on formal types

 

If you were to call GetGenericArguments() on an open generic type, you would get the generic parameter list ... also called formal parameters.

 

Type t = Type.GetType("MyGenericType`2");

Type[] genargs = t.GetGenericArguments();

 

The array genargs would actually contain the type T and U as defined in the class definition. You are limited to what you can actually call on this formal type. Attempting to access BaseType, DeclaringType, ReflectedType etc. would result in a NotSupportedException being thrown. This is also true if you attempt to get interfaces, methods or members on it.

 

 

3. Discovering constraints on parameters through reflection.

 

NETCF does not support the GetGenericParameterConstraints() API and as such you will not be able to look up the constraints on generic parameters of any generic type/method through reflection. Another mechanism that the full .NET framework provides for discovering this is through discovery on formal parameters, but the restrictions on formal parameters stated above in III.2 would prevent you from doing this on NETCF. This restriction is further elaborated in IV.2.

 

 

4. Can't resolve method signature ambiguities because of lack of reflection on open types.

 

Say I have a type definition that looks something as shown below.

 

public class Generic2P <T, U>

{

   public void method (T x) {}

   public void method (U y) {}

}

 

public class Generic1P <R>

{

   public void method<S>(S s)

   {

      Generic2P<R, S> foo = new Generi2P<R, S>();

      foo.method(s);

   }

}

 

If for the instantiation of Generic1P's method has the same type used for both its generic parameters R and S, then it would result in ambiguity as to which method would be called on Generic2P. For example if I have

 

Generic1P<int> bar = new Generic1P<int>();

bar.method<int>(5);

 

Then this would result in Generic2P<int, int> getting created and Generic2P<int, int>.method(int) being called. However there will be two methods with this exact same signature. Both NETCF and the full .NET framework will default to the same method, but what if that is not the method you want to call?

 

On the full .NET framework one could get methods on the open type Generic2P<T, U> and based on the formal parameter used as the argument in the method, you could fetch the right method and bind parameters/arguments to it and invoke it. However since we can not get methods or members on open types in NETCF we would not be able to do this and you would not be able to manually resolve signature ambiguities.

 

 

 

IV. Differences with the full .NET Framework


 

1. Lack of TargetException, TargetInvocationException, TargetParameterCountException.

 

NETCF does not support the above mentioned exceptions that are thrown on the full .NET framework when one attempts to call BindGenericParameters() with illegal arguments. On NETCF the following substitutions occur.

 

TargetException -> ArgumentNullException, InvalidProgramException

TargetInvocationException -> MissingMethodException

TargetParameterCountException -> ArgumentException.

 

 

2. Constraint hierarchy vs. Derivation hierarchy.

 

Consider the following type definitions.

 

public interface InterfaceA {}

public interface InterfaceB {}

public class BaseA: InterfaceA {}

public class InheritedA: BaseA, InterfaceB {}

 

public class BaseB<T>

   where T: InterfaceA

{}

 

public class InheritedB<T>

   where T: BaseB<T>, InterfaceB

{}

 

In the case of InheritedA, it inherits from BaseA that implements InterfaceA. This hierarchy would constitute the derivation hierarchy. In the case of InheritedB, its generic parameter is constrained to inheriting from BaseB<T>, whose generic parameter is constrained to implementing InterfaceA. This hierarchy would constitute the constraint hierarchy.

 

On the full .NET framework, if we were to get the formal type T via a call to GetGenericArguments() on the open type InheritedB; we could traverse the constraint hierarchy as if it were a derivation hierarch by using the BaseType property and calling GetInterfaces() method on it. In NETCF however we distinguish between the two and do not allow you to treat constraint hierarchy as if it were a derivation hierarchy. So you cannot use the BaseType property and the GetInterfaces() method to peruse the constraint hierarchy. Instead you would get a NotSupportedException thrown at you.

 

 

 

3. Differences in exceptions thrown.

 

There are many places where we throw MissingMethodExceptions instead of ArgumentExceptions and the like. Hopefully this will be addressed in another blog post that will be soon to come.

 


This posting is provided "AS IS" with no warranties, and confers no rights.

Here's an informative article about generics in the upcoming version of .NET Compact Framework by Roman.

http://weblogs.asp.net/romanbat/archive/2005/01/06/348114.aspx

I am in the process of writing an article about reflection support for generic types in the .NET Compact framework. It should be posted some time soon hopefully.

http://smartphonethemes.com/preview.asp?ID=1687

http://smartphonethemes.com/preview.asp?ID=1688

http://smartphonethemes.com/preview.asp?ID=1686

Just copy the xml and the image file to your \Windows directory. The theme should then show up under 

   Settings->Home Screen.

Its quite easy to create your own theme ... and you can create things that look so much better than PPC themes. The current wysiwg editor for SmartPhone themes does not let you create these kind of themes ... but it shouldn't be too hard to write one up.

2 Comments
Filed under: ,

Recently I have been looking into painless ways to do some amount of code injection for MSIL for fault tolerance and security testing. One of my requirements was to be able to do this dynamically. I had found some tools that would do it statically by creating wrapper dlls that would use Reflection, but these were a pain to use and had severe limitations (and bugs).

I found a link to this article on one of my google excursions and it caught my attention: Rewrite MSIL Code on the Fly with .Net Framework Profiling API by Aleksandir Mikunov. Besides being an exhaustive article, it certainly gives me ideas on approaching the whole dynamic fault injection space ...

Since the profiler can intercept the JIT compilation process, I could insert a custom prologue that would contain trigger logic to either execute the method or fail as I would want it to. It seems feasible enough ... but of course I haven't tried this out yet.

I am sure someone else has already thought about this ... so what I am now wondering if someone out there has already attempted a tool like this. If not ... hey, I have my next December project !

1 Comments
Filed under:

So I just got a query about how to create PPC themes. Well it's simple ... we have a tool ... Pocket PC theme generator !

http://www.microsoft.com/windowsmobile/downloads/themegenerator.mspx

5 Comments
Filed under: ,

A long time ago I made some NETCF themes for the Pocket PC ... so I thought I should share them out for all the customization fanatics out there. Have fun !

http://www.pocketpcthemes.com/preview.asp?ID=18517

http://www.pocketpcthemes.com/preview.asp?ID=18519

http://www.pocketpcthemes.com/preview.asp?ID=18520

http://www.pocketpcthemes.com/preview.asp?ID=18521

2 Comments
Filed under: ,

I would like to say something clever in my first post, but testing generics has my brain tied up in knots.

I am currently an SDET in .Net Compact Framework at Microsoft testing security and generics. Its been 2 years for me on this team and I have to say it is the best team I have worked with so far.

It has been customary for me to post some code on my first blog and I am not about to break that tradition today. So here it is:

 

#include <stdio.h>

main(int argc, char **argv)

{

while (*argv != argv[1] && (*argv = argv[1]) && (argc = 0) || (*++argv

&& (**argv && ((++argc)[*argv] && (**argv <= argc[*argv] ||

(**argv += argc[*argv] -= **argv = argc[*argv] - **argv)) &&

--argv || putchar(**argv) && ++*argv--) || putchar(10))))

;

}

 

You should be able to compile this code in VS. Try figuring out what it does ...

5 Comments
Filed under: ,
 
Page view tracker