Standing on the shoulders of giants. RSS 2.0
# Monday, April 11, 2011

When you forward a generic type to another assembly [0] using the TypeForwardedTo attribute, you can’t specify the typeparameters. Instead the number of typeparameters in in the class definition depends the number of comma’s in definition.

using System.Runtime.CompilerServices;

[assembly: TypeForwardedTo(typeof(Destination.SomeClass))] // Non generic

[assembly: TypeForwardedTo(typeof(Destination.SomeClass<>))] // equivalent to SomeClass<T>

[assembly: TypeForwardedTo(typeof(Destination.SomeClass<,>))] // equivalent to SomeClass<T,U>

More info on typeforwarding

Posted via email from Paul van Brenk's posterous

Monday, April 11, 2011 3:41:37 PM (Pacific Daylight Time, UTC-07:00)  #    Comments [0] - Trackback

# Tuesday, March 08, 2011

As both Jason and Soma blogged earlier, Visual Studio 2010 SP1 is released today.

MSDN subscriber can download Service Pack 1 today, everyone else has to have a little more patience until March 10, when they can download it from Microsoft downloads.

Posted via email from Paul van Brenk's posterous

Tuesday, March 08, 2011 3:33:05 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0] - Trackback

# Monday, February 28, 2011

I’ve been doing a lot of development using Visual Basic in the past couple of months and there are some interesting features in the language which may or may not be very well known. Features that don’t have an equivalent in C#.

Default values for auto implemented properties.

Visual Basic support the option of setting a default value for a property, like you would in a field.

Public Property MyProp As Integer = 42

MSDN: Auto-Implemented Properties

Key properties in anonymous types

By defining properties in anonymous types as key properties, you can enable specific behavior for the equality behavior of these types. When an anonymous type contains key properties, then for both the Equals and the GetHashCode overridde is generated based on these key properties. Additionally the key properties are readonly, where normal properties are read/write.

Dim one = New With {.Name = "one", Key .No = 1}

Dim two = New With {.Name = "two", Key .No = 1}

' This is true because the key property for both instances of the same class is equal

Assert.IsTrue(one.Equals(two))

MSDN: Key

Filtered exceptions

Adding a filter to a catch expression in a try/catch block allows you to direct the exception handling based on a condition. This is especially useful when you’re catching a general exception, which should be handled differently based on the error code it contains.

Try

Throw New COMException()

Catch ex As COMException When ex.ErrorCode = 0

Console.WriteLine("y = 0")

Catch ex As COMException When ex.ErrorCode <> 0

Console.WriteLine("y <> 0")

End Try

MSDN: Try...Catch...Finally Statement

Posted via email from Paul van Brenk's posterous

Monday, February 28, 2011 4:36:33 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0] - Trackback

# Tuesday, December 07, 2010

Today Jason Zander announced the availability of Visual Studio 2010 SP1 Beta on his blog. It’s available today for MSDN subscriber and will be general available on Thursday.

 

This service pack consists for the most part of fixes for issues logged through the connect site, but one noticeable new feature is the inclusion of the VB runtime compiler switch. This allows developers to ‘embed’ the Visual Basic Runtime in their assemblies, similar to No-PIA. The compiler figures out which parts of the runtime your program actually uses and embeds only these parts in the generated assemblies.

 

Download the service pack and report any issues you encounter, that’s the only way we can make it better for you. (Even if the issues you encounter aren’t fixed in the service pack, we still look at them and consider them for a next version of Visual Studio).

Posted via email from Paul van Brenk's posterous

Tuesday, December 07, 2010 11:47:06 AM (Pacific Standard Time, UTC-08:00)  #    Comments [0] - Trackback

# Thursday, November 18, 2010

You will not only hear what’s coming next for SilverLight from Scott Guthrie, but after all the other interesting session about databinding, performance, Windows Phone 7 development there’s the After Party! (Sorry, the After Party is not available online).

More info and registration on Silverlight.net for attending in Redmond and online.

Posted via email from Paul van Brenk's posterous

Thursday, November 18, 2010 1:54:49 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0] - Trackback

# Wednesday, June 23, 2010
So, it's been over a year..... which I know is a long time, but much has happened in a very short time, so blogging was one of the casualties. But now that things have settled down, I´m back!
 
So what has happened:
  • After interning at Microsoft last summer, I got a job offer for a position in the C# IDE QA Team, which I accepted.
  • I married my wife
  • We moved to the US (See 1)
  • Found a place to live
  • Worked hard on finishing my Bachelor's degree
  • Helped finish Visual Studio 2010 (a little)
What's next? We'll start working on dev 11 (VS v.Next) soon, which will be exciting since it's the first time I'll be involved in the entire product cycle from planning till release. Hopefully a lot more blogging/tweeting (140 chars should be enough for everyone).
 
btw. I'm posting this via posterous, it should show on my regular blog. (http://www.paulvanbrenk.com)
 
 
 

Posted via email from Paul van Brenk's posterous

Wednesday, June 23, 2010 4:30:41 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [0] - Trackback

# Friday, May 29, 2009

The new dynamic keyword and the DLR in C# 4 and the .NET Framework 4.0 can be used for good, as displayed by IronRuby, IronPython and several other samples.

But it’s much more to fun to use it for evil… by making Reflection simple.

In C# 3.0 invoking members through reflection, was kind of odd and certainly not very readable.

var employee = new Employee();
var members = employee.GetType().GetMember("age", MemberTypes.All,
                BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

((FieldInfo)members[0]).SetValue(employee, 30);

By wrapping all reflection magic in a dynamic object the same call would look like:

var employee = (new Employee()).AsDynamic();

employee.Name = "Paul van Brenk";
employee.Age = 30;

Console.WriteLine("Employee {0} is {1} years old.", employee.Name, employee.Age);

How this works is relatively easy.. by deriving a wrapper class from the new DynamicObject and overriding the TrySetMember and TryGetMember object to do the dirty work for you.

static class DynamicHelper
{
    public static dynamic AsDynamic(this T source)
    {
        return new DynamicReflection(source);
    }

    class DynamicReflection : DynamicObject
    {
        public DynamicReflection(T source)
            : base()
        {
            this.Source = source;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            // find the member
            MemberInfo member;
            if (!TryFindMember(binder.Name, out member))
            {
                return false;
            }

            // we can only set values to fields and properties
            // using reflection
            switch (member.MemberType)
            {
                /* todo: check the type of the incoming value and the type of 
                  the property. */
                case (MemberTypes.Field):
                    ((FieldInfo)member).SetValue(Source, value);
                    return true;
                case (MemberTypes.Property):
                    ((PropertyInfo)member).SetValue(Source, value,/*ndex*/ null); // we don't support indexed properties
                    return true;
            }

            // didn't work
            return false;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            // find the member
            MemberInfo member;
            if (!TryFindMember(binder.Name, out member))
            {
                result = null;
                return false;
            }

            // we can only set values to fields and properties
            // using reflection
            switch (member.MemberType)
            {
                /* todo: check the type of the incoming value and the type of 
                  the property. */
                case (MemberTypes.Field):
                    result = ((FieldInfo)member).GetValue(Source);
                    return true;
                case (MemberTypes.Property):
                    result = ((PropertyInfo)member).GetValue(Source,/*ndex*/ null); // we don't support indexed properties
                    return true;
            }

            // didn't work
            result = null;
            return false;
        }

        private bool TryFindMember(string name, out MemberInfo memberInfo)
        {
            // find the member
            var members = Type.GetMember(name, MemberTypes.All,
                                      BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

            // more than 1 not supported for now
            if (members.Length != 1) { memberInfo = null; return false; }

            memberInfo = members[0];
            return true;
        }

        private Type Type { get { return typeof(T); } }

        public T Source { get; private set; }
    }
}

More info about implementing dynamic objects and behavior can be found on the DLR CodePlex site (esp. Getting Started with the DLR as a Library Author).

Source: Program.cs.txt

Friday, May 29, 2009 10:53:08 PM (Pacific Daylight Time, UTC-07:00)  #    Comments [0] - Trackback
C# | Visual Studio 2010
Ads
About
© Copyright 2012
Paul van Brenk
Sign In
newtelligence dasBlog 2.3.2011.0
All Content © 2012, Paul van Brenk
DasBlog theme 'Business' created by Christoph De Baene (delarou)