Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Friday, January 27, 2012

Removing Strong Name from .NET assembly

First in 10+ years of .net practice I had to solve removing the strong name from the assembly and recompiling it after the export with Reflector.

The need for that was caused by customer’s extra security/audit requirements which could not be satisfied through a standard extensibility in MS CRM 3.0. And let us say MS CRM 3.0 is a very old lady now, so one can hardly expect year 2005 code to meet year 2012 security standards.

I found two solutions over the web:

http://blog.fredrikhaglund.se/blog/2009/02/06/how-to-patchfixhack-someone-elses-assembly/

http://www.codeproject.com/Articles/15374/Removing-Strong-Signing-from-assemblies-at-file-le

I was happy I didn’t have to resort to a second one – quite heavier weight for what I needed. First one worked as a charm.

By the way  I tried side by side latest .NET reflector from Red Gate with the last not expired version of original Reflector and should say that I didn’t notice any value added in Red Gate’s version. Nothing to advocate a sudden price tag and 1+ year of ownership, SHAME!

As for decompile and recompile of a CRM assembly in question, just few issues I ran into:

Compiler limit exceeded: Line cannot exceed 2046 characters (CS1034) - C:\CRM\PROJECTS\components.core\Microsoft\Crm\SystemCustomization\Security.cs:31,2046

That was solved easily with splitting the line into several instructions.

Microsoft.Crm.CalendarUtil:

Operator '>=' cannot be applied to operands of type 'System.DayOfWeek' and 'int' (CS0019) - C:\CRM\PROJECTS\components.core\Microsoft\Crm\CalendarUtil.cs:157,17

Corrected version of a method would look like (trick is in casting the enums to int):

public static DateTime GetFirstDayInCalendar(DateTime date)
{
    date = new DateTime(date.Year, date.Month, 1, 0, 0, 0);
    int weekStartDayCode = OrganizationSettings.Current.WeekStartDayCode;
    if ((int)date.DayOfWeek >= weekStartDayCode)
    {
        return date.AddDays((double) (((int)date.DayOfWeek - weekStartDayCode) * ~(int)DayOfWeek.Sunday));
    }
    return date.AddDays((double) ((((int)date.DayOfWeek + 7) - weekStartDayCode) * ~(int)DayOfWeek.Sunday));
}

And again, always something to learn! I was really puzzled with that tilda operator in front of DayOfWeek.Sunday so was glad to learn about it: http://stackoverflow.com/questions/387424/what-is-the-tilde-in-a-c-sharp-enumeration

This is it. Hope my positive experience will ease your decisions once you stay in front of a similar task!

Sunday, January 27, 2008

Exploration on Reflection for the anonymous types in C# 3.0

Although some of the information may be trivial, the purpose was just to look a bit under the hood.

Creating an anonymous Type.

in the XmlSink.Common.UnitTests.dll:

namespace XmlSink.Common.UnitTests
{
.......
var anObject = new { Id = 25, Name = "Test Name" };

// Also possible to create with projection,
// http://blogs.msdn.com/ericwhite/pages/Tuples-and-Anonymous-Types.aspx

Resulting type full name and fully qualified name.

<>f__AnonymousType0`2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], XmlSink.Common.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

What compiler adds to the assembly?

image

 

Nice: DebuggerDisplayAttribute gets generated for the type

Nice: Overriden ToString() method.

Trace.WriteLine(anObject.ToString());

{ Id = 25, Name = Test Name }

As this is just another type in the assembly - Access as to the standard type.

PropertyInfo[] pis = aType.GetProperties();

Assert.IsNotNull(pis);
Assert.AreEqual(2, pis.Length);

foreach (PropertyInfo pi in pis)
    Trace.WriteLine(String.Format(CultureInfo.InvariantCulture,
        "Property found {0} with value {1}",
        pi.Name, pi.GetValue(anObject, null)));

Produces the following expected output:

Property found Id with value 25
Property found Name with value Test Name

To be answered:
? How to safely find out if type is anonymous
? How and why at all to generate anonymous type dynamically

Links:
Good entry point: http://blogs.msdn.com/ericwhite/pages/Tuples-and-Anonymous-Types.aspx
Want to cast to/from the anonymous type? http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1145096&SiteID=1
Recommended to language geeks :) http://tomasp.net/articles/csharp3-concepts.aspx