Dynamically find the parameter to be passed as <T> to a generic method

Posted by Codex on Stack Overflow See other posts from Stack Overflow or by Codex
Published on 2010-06-17T09:12:18Z Indexed on 2010/06/17 9:13 UTC
Read the original article Hit count: 229

Filed under:
|
|
|
|

A generic method is defined as follows:

    private static T GetComparisonObject<T>(ComparisonAttribute attribute, object objectToParse)
    {

        // Perform a some action
        return (T)resultObject;
    }

The method is invoked as follows:

var srcObjectToCompare = GetComparisonObject<DynamicType>(attributeToCompare, srcObject);

The type for which the method needs to be invoked is configured in the config file as:

<add attributename ="Count" attributetype ="MemberInformation" attributeparam ="Count" type="System.Int32" comparertype="ditCreditEMGTestAutomationDifferenceEngine.Comparers.TypeComparer, ditCreditEMGTestAutomationDifferenceEngine.dll"  />

The token that is passed in <> for the generic methods has to be the type for which the method is being invoked. From the type key configuration in the XML, an instance of Type represnting the type can be created{i.e. Type.GetType("System.Int32")}, but how can the Type Definition be generated which can then be passed to the the Generic method?

Hope am not missing something elementary here!! :-O

Thanks in advance.

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics