Tuesday, November 15, 2016

Extension method for OrderBy




This is an extermely useful method that can be used to order the entity object while data binding.
Source : http://extensionmethod.net/csharp/ienumerable-t/orderby-string-sortexpression

Here is the code in VB.NET.


<Extension()> _
    Public Function OrderBy(Of T)(list As IEnumerable(Of T), sortExpression As String) As IEnumerable(Of T)
        sortExpression += ""
        Dim parts As String() = sortExpression.Split(" "c)
        Dim descending As Boolean = False
        Dim [property] As String = ""

        If parts.Length > 0 AndAlso parts(0) <> "" Then
            [property] = parts(0)

            If parts.Length > 1 Then
                descending = parts(1).ToLower().Contains("esc")
            End If

            Dim prop As PropertyInfo = GetType(T).GetProperty([property])

            If prop Is Nothing Then
                Throw New Exception((Convert.ToString("No property '") & [property]) + "' in + " + GetType(T).Name + "'")
            End If

            If descending Then
                Return list.OrderByDescending(Function(x) prop.GetValue(x, Nothing))
            Else
                Return list.OrderBy(Function(x) prop.GetValue(x, Nothing))
            End If
        End If

        Return list
    End Function

Tuesday, December 15, 2015

C# and AutoCAD - The debugging challenges


The most common error one encounters while trying to debug a c# command in ACAD from visual studio is "Unknown Command xxx". The reasons one will encounter this error are:


  • The Copy Local properties for the referenced dlls - 'acdbmgd.dll' and 'acmgd.dll' is set to true in the project. This has to be set to 'False'.

  • In the acad.exe.config in the Autocad installed folder, the supported run time version is not updated depending on the .NET assembly version.
        <startup useLegacyV2RuntimeActivationPolicy="true">
               <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
                         <supportedRuntime version="v2.0.50727"></supportedRuntime>
               </startup>

        • The other reason could be that there are conflicting commands in the command file.