semper fi
semper fi

Reputation: 737

Call generic method c#

how to call generic method, I heard about reflection, but I think I'm doing some wrong, check please.

 public static LinkedListNode<T> MergeSortLL<T>(LinkedListNode<T> Head) where T : IComparable<T>
    {

        if (Head == null || Head.Next == null)
        {
            return Head;
        }
        LinkedListNode<T> middle = GetMiddle<T>(Head);
        LinkedListNode<T> half = middle.Next;
        middle.Next = null;

        return Merge(Head, half);

    }

And here i'm trying to invoke my generic method with parameters

    public void MSort()
    {
        Type type = typeof(MergeSort);
        MethodInfo method = typeof(MergeSort).GetMethod("MergeSortLL");
        MethodInfo generic = method.MakeGenericMethod(type);
        generic.Invoke(null, null);
    }

Update 2. Here more info what I've in my class MergeSort and why i'm using IComparable

class MergeSort
{

    public static LinkedListNode<T> MergeSortLL<T>(LinkedListNode<T> Head) where T : IComparable<T>
    {

        if (Head == null || Head.Next == null)
        {
            return Head;
        }
        LinkedListNode<T> middle = GetMiddle<T>(Head);
        LinkedListNode<T> half = middle.Next;
        middle.Next = null;

        return Merge(Head, Head, half);

    }

    public static LinkedListNode<T> Merge<T>(LinkedListNode<T> Head, LinkedListNode<T> Left, LinkedListNode<T> Right) where T : IComparable<T>
    {

        LinkedListNode<T> mHead = Head;
        LinkedListNode<T> curr = mHead;

        while (Left != null && Right != null)
        {
            if (Left.Value.CompareTo(Right.Value) <= 0)
            {
                curr.Next = Left;
                Left = Left.Next;
            }
            else
            {
                curr.Next = Right;
                Right = Right.Next;
            }
            curr = curr.Next;
        }
        curr.Next = (Left == null) ? Right : Left;

        return mHead.Next;
    }

    public static LinkedListNode<T> GetMiddle<T>(LinkedListNode<T> Head) where T : IComparable<T>
    {
        if (Head == null)
        {
            return Head;
        }

        LinkedListNode<T> slow, fast;
        slow = fast = Head;
        while (fast.Next != null && fast.Next.Next != null)
        {
            slow = slow.Next; fast = fast.Next.Next;
        }
        return slow;
    }

}

Upvotes: 0

Views: 172

Answers (2)

juharr
juharr

Reputation: 32266

If you have a LinkedList<T> just pass it's head (via the First property) to the method and the types will be inferred.

var list = new LinkedList<int>();
var head = list.First; 
var sorted = MergeSort.MergeSortLL(head);

Assuming that MergeSort is a static class you could make it an extension method by adding this before the LinkedListNode<T> Head parameter and then it could be called as if it were an instance method.

var sorted = head.MergeSortLL();

Upvotes: 2

Callum Linington
Callum Linington

Reputation: 14417

Looking at the comments you're leaving and the issues you are running into. It seems to me that you're intention is to pass the LinkedList to this method, so it should be:

public static LinkedListNode<T> MergeSortLL<T>(LinkedList<T> Head) where T : IComparable<T>

Then you should just be invoked like this:

var head = new LinkedList<int>();

MergeSortLL<int>(head)

You see I've set the int as the type I want the method to be constrained to.

However, because you will have a strongly typed LinkedList the C# compiler will implicitly set the <int> on the MergeSortLL method reducing the call to:

MergeSortLL(head);

You've also put a generic constraint on the genereic type T, make sure that whatever type your are using inherits from IComparable<T> which when saying out loud sounds a little strange, but seems right.

Upvotes: 3

Related Questions