LinQ XML mapping to a generic type

Posted by Manuel Navarro on Stack Overflow See other posts from Stack Overflow or by Manuel Navarro
Published on 2009-08-24T23:28:54Z Indexed on 2010/04/05 14:03 UTC
Read the original article Hit count: 253

Filed under:
|
|

I´m trying to use an external XML file to map the output from a stored procedure into an instance of a class. The problem is that my class is of a generic type:

public class MyValue<T>
{
  public T Value
  {
    get;
    set;
  }
}

Searching through a lot of blogs an articles I've managed to get this:

<?xml version="1.0" encoding="utf-8" ?>
<Database Name="" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007">
    <Table Name="MyValue" Member="MyNamespace.MyValue`1" >
    	<Type Name="MyNamespace.MyValue`1">
    		<Column Name="Category" Member="Value" DbType="VarChar(100)" />
    	</Type>
    </Table>
    <Function Method="GetResourceCategories" Name="myprefix_GetResourceCategories" >
    	<ElementType Name="MyNamespace.MyValue`1"/>
    </Function>
</Database>

The MyNamespace.MyValue`1 trick works fine, and the class is recognized. I expect four rows from the stored procedure, and I'm getting four MyValue<string> instances, but the big problem is that the property Value for the all four instances is null. The property is not getting mapped and I don't really get why. Maybe worth noting that the property Value is generic, and that when the mapping is done using attributes it works perfect.

Anyone have a clue?

BTW the method GetResourceCategories:

public ISingleResult<MyValue<string>> GetResourceCategories()
{
  IExecuteResult result = this.ExecuteMethodCall(
    this,
    (MethodInfo)MethodInfo.GetCurrentMethod());

  return (ISingleResult<MyValue<string>>)result.ReturnValue;
}

© Stack Overflow or respective owner

Related posts about .NET

Related posts about LINQ