返回 T 的对象实例的通用方法

发布于 2024-12-28 19:09:02 字数 573 浏览 4 评论 0原文

我有相同的类,但在不同的命名空间中。例如:x.InHeaderType、y.InHeaderType 等...

我必须经常使用相同的参数创建这些类的实例。

这是一个示例:

x.InHeaderType inHeader = new x.InHeaderType();

inHeader.CompanyId = "TEST";
inHeader.UserId = "TEST";
inHeader.Password = "TEST";
inHeader.MessageId = " ";

是否可以创建如下方法:

    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
    }

提前致谢,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泛泛之交 2025-01-04 19:09:02

您可以使用以下代码:

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

这是 Tomas Jansson 答案的简化版本。它假设所有类型都有一个默认构造函数。

你可以这样称呼它:

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

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>();
绮烟 2025-01-04 19:09:02

首先,为什么两个不同的命名空间中有相同的类?

我会创建某种神奇的功能,例如:

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

这可能有用,但我不确定我会推荐它。您的代码可能存在其他类型的问题,迫使您执行此类操作,您应该首先修复这些问题。

更新:我假设你不能让对象继承相同的接口。如果可以的话你绝对不应该使用上面的代码,如果可以的话你应该使用下面的代码:

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

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;
}
倾城花音 2025-01-04 19:09:02

是的,您必须使用要设置的属性定义一个接口或基类,从中专门化您的特定类型,并使用泛型约束让编译器知道泛型类型参数具有这些属性。

详细信息:http://msdn.microsoft.com/en-us/library/ d5x73970.aspx

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

白色秋天 2025-01-04 19:09:02

这段代码有味道。拥有两种完全相同但位于不同命名空间中的类型并不是一个好主意。您可以将它们移至公共共享库吗?

我发现您正在使用服务参考。您需要每个端点的引用吗?您能否以某种方式重构您的引用以删除此重复项?

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?

歌枕肩 2025-01-04 19:09:02

我认为你需要:

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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文