Generic method returning an object instance of T

18,049

Solution 1

You can use the following code:

public static T GetInHeaderProperty<T>() where T : new()
{
    dynamic result = new T();
    result.CompanyId = "Test";
    result.UserId = "Test";
    result.Password = "Test";
    result.MessageId = " ";
    return (T)result;
}

This is a simplified version of Tomas Jansson's answer. It assumes that all the types have a default constructor.

You can call it like this:

var result = GetInHeaderProperty<x.InHeaderType>();

Solution 2

First of, why do you have the same class in two differente namespaces?

I would create somekind of magic function like:

public static T GetInHeaderProperty<T>(Func<T> createObjFunc)
{
    T result = createObjFunc();
    dynamic resultAsDynamic = result as dynamic;
    resultAsDynamic.CompanyId = "Test";
    resultAsDynamic.UserId = "Test";
    resultAsDynamic.Password = "Test";
    resultAsDynamic.MessageId = " ";
    return (T)result;
}

That might work, but I'm not sure I would recommend it. You might have some other kind of issue with your code forcing you to do things like this that you should fix first.

UPDATE: I made the assumption that you can't make the objects inherit the same interface. If they can you should definitely not use the code above, if they can you should use the code below instead:

public static T GetInHeaderProperty<T>() where T : ISomeType, new()
{
    T result = new T();
    result.CompanyId = "Test";
    result.UserId = "Test";
    result.Password = "Test";
    result.MessageId = " ";
    return result;
}

Solution 3

Yes, you'd have to define an interface or base class with the properties you want to set, specialize your specific types from that, and use generic constraints to let the compiler know that the generic type parameter has those properties.

More info: http://msdn.microsoft.com/en-us/library/d5x73970.aspx

Solution 4

This code smells. Having 2 types exactly the same but in different namespaces isn't a great idea. Can you move them to a common shared lib?

I see you're using service references. Do you need to have references to each endpoint? Could you somehow refactor your references to remove this duplication?

Share:
18,049
anilca
Author by

anilca

mostly made of water

Updated on June 04, 2022

Comments

  • anilca
    anilca almost 2 years

    I have same classes but in different namespaces. ex: x.InHeaderType, y.InHeaderType etc...

    I have to create instances of these classes with same parameters frequently.

    Here is a sample:

    x.InHeaderType inHeader = new x.InHeaderType();
    
    inHeader.CompanyId = "TEST";
    inHeader.UserId = "TEST";
    inHeader.Password = "TEST";
    inHeader.MessageId = " ";
    

    Is it possible to create a method like:

        public static T GetInHeaderProperty<T>()
        {
            T value;
            // fill the properties of object and return the instance of T
            // I will call it when I need x.InHeaderType or y.InHeaderType
        }
    

    Thanks in advance,