Getting a strange error with sorting DataGridView after encrypting my code

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
I am trying to utilize DataGridView bounded to a list of objects and was trying to figure out a way to handle sorting.

Doing some research online I came across a post where someone has posted a solution which involves extending the BindingList class to override the ApplySortCore method, among a few other things, and utilizes some basic Reflection to accomplish the task

This was working great on my normal builds but then when it came time to deploy my code I use CodeVeil from Xheo to encrypt my assembly. This never resulted in a problem for me before and I often use Reflection to set fields, properties and invoke methods
or instantiate classes, many times on string names. But now after I encrypted this particular assembly the sorting breaks with the following exception:


<div style="color:black; background-color:white
<pre>See the end of <span style="color:blue this

message <span style="color:blue for

details <span style="color:blue on

invoking

just-<span style="color:blue in

-time (JIT) debugging instead of <span style="color:blue this

dialog box.



************** Exception Text **************

System.MissingMethodException: Method not found: <span style="color:#a31515 Int32 GridSortTest.SortableBindingList`1.<ApplySortCore>b__0(**UNKNOWN TYPE**, **UNKNOWN TYPE**)

.

at GridSortTest.SortableBindingList`1.ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)

at System.ComponentModel.BindingList`1.System.ComponentModel.IBindingList.ApplySort(PropertyDescriptor prop, ListSortDirection direction)

at System.Windows.Forms.DataGridView.DataGridViewDataConnection.Sort(DataGridViewColumn dataGridViewColumn, ListSortDirection direction)

at System.Windows.Forms.DataGridView.SortInternal(IComparer comparer, DataGridViewColumn dataGridViewColumn, ListSortDirection direction)

at System.Windows.Forms.DataGridView.Sort(DataGridViewColumn dataGridViewColumn, ListSortDirection direction)<br/>







[/code]


But I really have no clue why its throwing me this error. It is definitely something that happens after the code is encrypted, as the normal exe is fine yet the encrypted one produces this error.

Does anyone know a workaround I could use or provide me with an alternative for sorting or be able to explain what is going on with this code?

All my code attached below:


<pre lang="x-c# using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Linq.Expressions;
using System.Reflection;

namespace GridSortTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
SortableBindingList<MyObject> list = new SortableBindingList<MyObject>();
list.Add(new MyObject("ghi", 3, 2.2));
list.Add(new MyObject("def", 2, 13.65));


list.Add(new MyObject("mno", 5, 47.13));
list.Add(new MyObject("abc", 1, 56.5));
list.Add(new MyObject("jkl", 4, 17.0));

DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = "Name";
col.DataPropertyName = "Name";
grid.Columns.Add(col);

col = new DataGridViewTextBoxColumn();
col.HeaderText = "ID";
col.DataPropertyName = "ID";
grid.Columns.Add(col);

col = new DataGridViewTextBoxColumn();
col.HeaderText = "Value";
col.DataPropertyName = "Value";
grid.Columns.Add(col);

grid.DataSource = list;
}
}

public class MyObject
{
public string name;
public int id;
public double value;

public MyObject(string name, int id, double value)
{
this.name = name;
this.id = id;
this.value = value;
}

public string Name { get { return name; } }
public int ID { get { return id; } }
public double Value { get { return value; } }
}

public class SortableBindingList<T> : BindingList<T>
{
protected override bool SupportsSortingCore
{
get { return true; }
}

private ListSortDirection sortDirection;

protected override ListSortDirection SortDirectionCore
{
get { return sortDirection; }
}

private PropertyDescriptor sortProperty;
protected override PropertyDescriptor SortPropertyCore
{
get { return sortProperty; }
}

ArrayList sortedList, unsortedItems;
bool isSortedValue = false;

protected override bool IsSortedCore
{
get { return isSortedValue; }
}


protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
Console.WriteLine(direction);
sortedList = new ArrayList();
sortProperty = prop;

// Check to see if the property type we are sorting by implements
// the IComparable interface.
Type interfaceType = prop.PropertyType.GetInterface("IComparable");

if (interfaceType != null)
{
// If so, set the SortPropertyValue and SortDirectionValue.
sortProperty = prop;
sortDirection = direction;

unsortedItems = new ArrayList(this.Count);

// Loop through each item, adding it the the sortedItems ArrayList.
foreach (Object item in this.Items) {
sortedList.Add(prop.GetValue(item));
unsortedItems.Add(item);
}
// Call Sort on the ArrayList.
sortedList.Sort();
T temp;

// Check the sort direction and then copy the sorted items
// back into the list.
if (direction == ListSortDirection.Descending)
{
sortedList.Reverse();
}

for (int i = 0; i < this.Count; i++)
{
int position = Find(prop.Name, sortedList);
if (position != i)
{
temp = this;
this = this[position];
this[position] = temp;
}
}

isSortedValue = true;

// Raise the ListChanged event so bound controls refresh their
// values.
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
else
// If the property type does not implement IComparable, let the user
// know.
throw new NotSupportedException("Cannot sort by " + prop.Name +
". This" + prop.PropertyType.ToString() +
" does not implement IComparable");


}

public int Find(string property, object key)
{
// Check the properties for a property with the specified name.
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(T));
PropertyDescriptor prop = properties.Find(property, true);

// If there is not a match, return -1 otherwise pass search to
// FindCore method.
if (prop == null)
return -1;
else
return FindCore(prop, key);
}

protected override int FindCore(PropertyDescriptor prop, object key)
{
// Get the property info for the specified property.
PropertyInfo propInfo = typeof(T).GetProperty(prop.Name);
T item;

if (key != null)
{
// Loop through the items to see if the key
// value matches the property value.
for (int i = 0; i < Count; ++i)
{
item = (T)Items;
if (propInfo.GetValue(item, null).Equals(key))
return i;
}
}
return -1;
}

protected override void RemoveSortCore()
{
int position;
object temp;
// Ensure the list has been sorted.
if (unsortedItems != null)
{
// Loop through the unsorted items and reorder the
// list per the unsorted list.
for (int i = 0; i < unsortedItems.Count; )
{
position = this.Find("LastName",
unsortedItems.GetType().
GetProperty("LastName").GetValue(unsortedItems, null));
if (position > 0 && position != i)
{
temp = this;
this = this[position];
this[position] = (T)temp;
i++;
}
else if (position == i)
i++;
else
// If an item in the unsorted list no longer exists,
// delete it.
unsortedItems.RemoveAt(i);
}
isSortedValue = false;
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
}

public void RemoveSort()
{
RemoveSortCore();
}


}
}
[/code]

View the full article
 
Back
Top