C# 使用泛型类作为非泛型类C的属性#
假设我有以下两个类,它们理想地满足我的需求: Foo.cs:C# 使用泛型类作为非泛型类C的属性#,c#,generics,C#,Generics,假设我有以下两个类,它们理想地满足我的需求: Foo.cs: public class Foo { private int Type { get; set; } = 0; public FooHelper FooHelper { get { switch (Type) { case 1: return new FooHe
public class Foo
{
private int Type { get; set; } = 0;
public FooHelper FooHelper
{
get
{
switch (Type)
{
case 1:
return new FooHelper<Guid>("Foo", "Bar", "foobar", "", Guid.NewGuid());
case 2:
default:
return new FooHelper<int>("Bar", "Foo", "", "", 11);
}
}
}
public Foo(int type)
{
Type = type;
}
}
public class FooHelper<T> where T : struct
{
public string Controller { get; set; } = string.Empty;
public string Action { get; set; } = string.Empty;
public string Area { get; set; } = string.Empty;
public string NameParameter { get; set; } = string.Empty;
public T IdParameter { get; set; } = default(T);
public FooHelper() { }
public FooHelper(string controller, string action, string area, string name, T id)
{
Controller = controller;
Action = action;
Area = area;
NameParameter = name;
IdParameter = id;
}
}
公共类Foo
{
私有int类型{get;set;}=0;
公共食物助手食物助手
{
得到
{
开关(类型)
{
案例1:
返回新的FooHelper(“Foo”、“Bar”、“foobar”、“Guid.NewGuid());
案例2:
违约:
返回新的FooHelper(“Bar”、“Foo”、“11”);
}
}
}
公共Foo(int类型)
{
类型=类型;
}
}
FooHelper.cs:
public class Foo
{
private int Type { get; set; } = 0;
public FooHelper FooHelper
{
get
{
switch (Type)
{
case 1:
return new FooHelper<Guid>("Foo", "Bar", "foobar", "", Guid.NewGuid());
case 2:
default:
return new FooHelper<int>("Bar", "Foo", "", "", 11);
}
}
}
public Foo(int type)
{
Type = type;
}
}
public class FooHelper<T> where T : struct
{
public string Controller { get; set; } = string.Empty;
public string Action { get; set; } = string.Empty;
public string Area { get; set; } = string.Empty;
public string NameParameter { get; set; } = string.Empty;
public T IdParameter { get; set; } = default(T);
public FooHelper() { }
public FooHelper(string controller, string action, string area, string name, T id)
{
Controller = controller;
Action = action;
Area = area;
NameParameter = name;
IdParameter = id;
}
}
公共类FooHelper,其中T:struct
{
公共字符串控制器{get;set;}=string.Empty;
公共字符串操作{get;set;}=string.Empty;
公共字符串区域{get;set;}=string.Empty;
公共字符串名称参数{get;set;}=string.Empty;
公共T IdParameter{get;set;}=默认值(T);
公共FooHelper(){}
公共FooHelper(字符串控制器、字符串操作、字符串区域、字符串名称、字符串id)
{
控制器=控制器;
行动=行动;
面积=面积;
名称参数=名称;
IdParameter=id;
}
}
根据作为非泛型类Foo构造函数的参数传入的整数值,属性fooheloper基于Guid或int返回泛型类fooheloper的新实例
然而,如果我不把Foo也变成泛型的话,我似乎就无法做到这一点,这是我不想要的。我得到的错误在FooHelper的属性行上,指出:
使用泛型类型“FooHelper”需要1个类型参数
这是有道理的,但我当时不知道。我想确定Foo的构造函数中fooohelper的类型
在这种情况下,我是缺少了什么,还是无法实现我想要的功能?问题是public
fooheloper-fooheloper
甚至不会编译,因为没有名为fooheloper
的类型。然而,有fooheloper
和fooheloper
这两个词却没有任何共同之处。因此,这个问题(再次)是由于您在运行时提供了一个信息(类型开关),并期望编译器在编译时推断出正确的泛型类型,这是不可能的
相反,您可以创建一个非泛型接口,您的所有助手都从该接口派生并返回该接口:
public interface IFooHelper { ... }
public class FooHelper<T> : IFooHelper where T : struct { ... }
但是,正如上面提到的,在编译时,无法知道实际的泛型类型参数是什么,因为该类型甚至不存在。我没有看到任何“ProfileHelper”,您是否知道泛型类对t有约束?(
其中T:struct
)?这与op的问题无关,因为他的代码@Romanananyev目前没有任何问题,谢谢您的解释。这完全有道理。我想我必须解决这个问题,因为接口解决方案也不是很可行(比如:泛型类型参数不能从接口属性访问)。