当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Simplest way to Convert an existing Method to generic object Type

问题描述:

Let's assume I have an existing method which ALWAYS returns a variable of type OldClassType

//Takes 1 parameter p1

//Returns a variable of type OldClassType, after doing a bunch of logic.

protected OldClassType MyMethod(int32 p1)

{

OldClassType myVar = new OldClassType();

myVar.name = "Hello";

myVar.age = "36";

return myVar;

}

Now, I have a NewClassType which has exactly the same properties as OldClassType but is of a different namespace. I need to convert the same method (MyMethod) so that instead of ALWAYS returning OldClassType, it should return a variable of type either OldClassType or NewClassType - based on a newly added parameter useNewType

So something like :

//Takes 2 parameter: p1 and useNewType - which determines when to use the NewClassType or default to the OldClassType

//Returns a variable of type object, after doing a bunch of logic. I'll modify the Call to this new modified method to cast the object to the appropriate Type (OldClassType/NewClassType)

protected object MyMethod(int32 p1, bool useNewType)

{

object myVar = new object();

myVar.name = "Hello"; // Do I use PropertyInfo and then set value?

myVar.age = "36";

return myVar;

}

EDIT:

One additional piece of information is that the OldClassType will be removed from the code in the future. So I'm trying to find the solution with least impact.

I was in fact wondering if the use of dynamic will solve it , ie minimize the changes to support NewClassType and when will the time to remove the OldClassType, I can simple change one line where I init it- example

protected dynamic MyMethod(int32 p1, bool useNewType)

{

dynamic myVar = (useNewType) ? new NewClassType() : new OldClassType(); // Is this allowed?

//So all code below stays exactly the same

myVar.name = "Hello";

myVar.age = "36";

return myVar;

}

网友答案:

You could make NewClassType derive from OldClassType so that it inherits all of its base type members.

public class NewClassType : OldClassType { }

You'd then have to make your method Generic, like this:

protected T MyMethod<T>(int p1) where T : OldClassType, new()
{
    T myVar = new T();
    myVar.name = "Hello";
    myVar.age = "36";

    return myVar;
}

I don't think you should use a bool value to specify if you want to return an instance of your new class type, you can just do:

var o = MyMethod<NewClassType>(10);

You could even define an overloaded method that still returns OldClassType but uses your generic method under the covers:

protected OldClassType MyMethod(int p1)
{
    return MyMethod<OldClassType>(p1);
}
网友答案:

I would avoid reflection, it is tricky and potentially slow.

Add a constructor to NewClassType that copies an OldClassType:

class NewClassType {
    public NewClassType( OldClassType old )
    {
        Name = old.Name;
        Age = old.Age;
        . . . 
    }
    . . . 
}

and then:

protected object MyMethod(int32 p1, bool useNewType)
{  
    OldClassType myVar = new OldClassType();
    myVar.name = "Hello";
    myVar.age = "36";

    if (!useNewType) return myVar; 
    return new NewClassType( myVar );
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: