3

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,

4
  • 5
    Why do you have such duplication? If you eliminate it, your problem is solved. Commented Jan 24, 2012 at 12:23
  • What @Oded said. Or, do they at least share a common base class you could use? Commented Jan 24, 2012 at 12:26
  • In fact they are not my classes, they are coming from several service references. Commented Jan 24, 2012 at 12:27
  • Are they endpoints that you have control over (e.g. in-house code?) Commented Jan 24, 2012 at 12:35

5 Answers 5

9

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>();
Sign up to request clarification or add additional context in comments.

Comments

8

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;
}

6 Comments

The line result = (T)resultAsDynamic; is not needed.
@DanielHilgarth, they are needed if the create object function does not create an object that conforms to an interface, which is the case since I assume the two class doesn't share the same interface or base class. See my second alternative if you can get both objects to inherit an interface.
Hmm...How will I call it? How can I give "Func<T> createObjFunc" parameter to function?
Not correct. resultAsDynamic and result both represent the same object. resultAsDynamic.Password = "Test"; Assert.AreSame(result.Password, "Test");.
@anilca, you would call the first one with GetInHeaderProperty(() => new SomeObject(maybeAParameter)). This opens up for great flexibility, like when you already have an object you could use: GetInHeaderProperty(() => existingObject).
|
2

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

Comments

1

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?

2 Comments

In fact they are not my classes, they are coming from several service references.
No I don't need each of them. I'm just adding these service references for fun. Thanks for your advice.
0

I think you need:

public interface IInHeaderType
{
 string CompanyId { get; set; }
 string UserId { get; set; }
 string Password { get; set; }
 string MessageId { get; set; }
}

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.