views:

53

answers:

2

I'm fairly new to generics and I'm having some trouble understanding parts of how they work and also failing to get it to work the way I want.

So far, I have this;

public interface IObjectMapper
{
    T MapObject<T>() where T : new();
}

public class CustomObjectMapper : IObjectMapper
{
    T IObjectMapper.MapObject<T>()
    {
        T retObject = new T();
        //do stuff......
    }
}

This works fine, but I don't understand what "where T : new()" is doing. Can someone please explain? Also, for my second question - I want a second method called DemapObject that takes 2 parameters, the same generic type T from object and then a different generic type of U - U should also be the return type.

public interface IObjectMapper
{
    T MapObject<T>() where T : new();
    U DemapObject<T, U>() ??????? what goes here?
}

Finally, once I get the interface method for DemapObject done - how is it called from the implementation class?

public class CustomObjectMapper : IObjectMapper
{
    NameValueCollection IObjectMapper.DempaObject<T, NameValueCollection>()
    {
        //is this right????????????
    }
}
+2  A: 

"where T :" will set a constraint on T. The constraint in this case is "new()". This means that the generic type T must have a constructor which takes no parameters.

You can stack where clauses on the same type:

  class MyClass<T> where T : class, new() { }

or on different types:

  class MyClass<T,U>
  where T : class, new()
  where U : IMyInterface
  {
  }
vanja.
+1 for answering partially and so quickly. Thank you!
WesleyJohnson
+1  A: 

where T: new() requires that any type used as T should implement default (argument-less constructor). Otherwise it will be impossible to create an instance of T class.

For your last question: you can't make a specialized implementation of generic method. But you can make a specialized implementation of interface. Consider this:

public interface IObjectMapper<U> where U:new
{
    T MapObject<T>(U arg) where T:new()
    U DemapObject<T>(T arg);
}
public class CustomObjectMapper : IObjectMapper<NameValueCollection>
{
    T MapObject<T>(NameValueCollection arg) where T:new(){
       ....
    }

    NameValueCollection DemapObject<T>(T arg)
    {
          ....
    }
}


IObjectMapper<NameValueCollection> mapper = new CustomObjectMapper();

And I haven't understood your second question, can you elaborate?

elder_george
I'm not sure I asked the question correctly, but looking at your code I think you solved it anyway. I'm trying to create an interface for mapping objects - sort of like ORM stuff.So basically the DemapObject method would take a generic object and strip out it's values, stuff them into the return type and spit it back out.So one implementation might have a return type of DataTable for DemapObject, and another implementation may have a return type of NameValueCollection. Yet another just might return a string with all the values tossed in it for querystring purposes.
WesleyJohnson