C# 在c中访问函数中泛型类型的属性#

C# 在c中访问函数中泛型类型的属性#,c#,generics,C#,Generics,我需要一个具有泛型方法的接口。但是,该接口的每个实现都知道其类型 class Program { static void Main(string[] args) { var specificContext = new SpecificContext(); var res = new SrvThatCantBeGeneric().GetValueFromSpecificContext(specificContext); Conso

我需要一个具有泛型方法的接口。但是,该接口的每个实现都知道其类型

class Program
{
    static void Main(string[] args)
    {

        var specificContext = new SpecificContext();

        var res = new SrvThatCantBeGeneric().GetValueFromSpecificContext(specificContext);
        Console.WriteLine(res);
    }

}
public class SrvThatCantBeGeneric : ISrvThatCantBeGeneric
{
    public int GetValueFromSpecificContext<SpecificContext>(SpecificContext specificContext)
    {
        return specificContext.MyProperty; // <-- this is where it breaks
    }
}

public interface ISrvThatCantBeGeneric
{        
    int GetValueFromSpecificContext<T>( T specificContext);
}

public class SpecificContext
{
    public int MyProperty { get; set; } = 42;
}
类程序
{
静态void Main(字符串[]参数)
{
var specificContext=new specificContext();
var res=new SrvThatCantBeGeneric().GetValueFromSpecificContext(specificContext);
控制台写入线(res);
}
}
公共类SrvThatCantBeGeneric:ISrvThatCantBeGeneric
{
public int GetValueFromSpecificContext(SpecificContext SpecificContext)
{
通过写入以下内容返回specificContext.MyProperty;//:

public int GetValueFromSpecificContext<SpecificContext>(SpecificContext specificContext)
也许你想写:

public interface ISrvThatCantBeGeneric
{
  int GetValueFromSpecificContext<T>(T specificContext) where T : SpecificContext;
}

public class SrvThatCantBeGeneric : ISrvThatCantBeGeneric
{
  public int GetValueFromSpecificContext<T>(T specificContext) where T : SpecificContext
  {
    return specificContext.MyProperty;
  }
}

public class SpecificContext
{
  public int MyProperty { get; set; } = 42;
}

public class SpecificContextChild : SpecificContext
{
  public SpecificContextChild()
  {
    MyProperty = 10;
  }
}
输出

您还可以将接口和类升级为泛型,而不是方法本身

如果相关:

public interface ISrvThatCantBeGeneric<T> where T : SpecificContext
{
  int GetValueFromSpecificContext(T specificContext);
}

public class SrvThatCantBeGeneric<T> : ISrvThatCantBeGeneric<T> where T : SpecificContext
{
  public int GetValueFromSpecificContext(T specificContext)
  {
    return specificContext.MyProperty;
  }
}

public class SpecificContext
{
  public int MyProperty { get; set; } = 42;
}

public class SpecificContextChild : SpecificContext
{

  public SpecificContextChild()
  {
    MyProperty = 10;
  }
}

var server = new SrvThatCantBeGeneric<SpecificContextChild>();
var context = new SpecificContextChild();
Console.WriteLine(server.GetValueFromSpecificContext(context));
非泛型类版本:

公共接口是vthatCantBegeneric
{
int GetValueFromSpecificContext(T specificContext),其中T:IContext;
}
公共类SrvThatCantBeGeneric:ISrvThatCantBeGeneric
{
public int GetValueFromSpecificContext(T specificContext),其中T:IContext
{
返回specificContext.MyProperty;
}
}
公共类特定上下文:IContext
{
公共int MyProperty{get;set;}=42;
}
通用类版本:

公共接口是vthatCantBegeneric,其中T:IContext
{
int GetValueFromSpecificContext(T specificContext);
}
公共类SrvThatCantBeGeneric:ISrvThatCantBeGeneric其中T:IContext
{
public int GetValueFromSpecificContext(T specificContext)
{
返回specificContext.MyProperty;
}
}
公共类特定上下文:IContext
{
公共int MyProperty{get;set;}=42;
}
通过书写:

public int GetValueFromSpecificContext<SpecificContext>(SpecificContext specificContext)
也许你想写:

public interface ISrvThatCantBeGeneric
{
  int GetValueFromSpecificContext<T>(T specificContext) where T : SpecificContext;
}

public class SrvThatCantBeGeneric : ISrvThatCantBeGeneric
{
  public int GetValueFromSpecificContext<T>(T specificContext) where T : SpecificContext
  {
    return specificContext.MyProperty;
  }
}

public class SpecificContext
{
  public int MyProperty { get; set; } = 42;
}

public class SpecificContextChild : SpecificContext
{
  public SpecificContextChild()
  {
    MyProperty = 10;
  }
}
输出

您还可以将接口和类升级为泛型,而不是方法本身

如果相关:

public interface ISrvThatCantBeGeneric<T> where T : SpecificContext
{
  int GetValueFromSpecificContext(T specificContext);
}

public class SrvThatCantBeGeneric<T> : ISrvThatCantBeGeneric<T> where T : SpecificContext
{
  public int GetValueFromSpecificContext(T specificContext)
  {
    return specificContext.MyProperty;
  }
}

public class SpecificContext
{
  public int MyProperty { get; set; } = 42;
}

public class SpecificContextChild : SpecificContext
{

  public SpecificContextChild()
  {
    MyProperty = 10;
  }
}

var server = new SrvThatCantBeGeneric<SpecificContextChild>();
var context = new SpecificContextChild();
Console.WriteLine(server.GetValueFromSpecificContext(context));
非泛型类版本:

公共接口是vthatCantBegeneric
{
int GetValueFromSpecificContext(T specificContext),其中T:IContext;
}
公共类SrvThatCantBeGeneric:ISrvThatCantBeGeneric
{
public int GetValueFromSpecificContext(T specificContext),其中T:IContext
{
返回specificContext.MyProperty;
}
}
公共类特定上下文:IContext
{
公共int MyProperty{get;set;}=42;
}
通用类版本:

公共接口是vthatCantBegeneric,其中T:IContext
{
int GetValueFromSpecificContext(T specificContext);
}
公共类SrvThatCantBeGeneric:ISrvThatCantBeGeneric其中T:IContext
{
public int GetValueFromSpecificContext(T specificContext)
{
返回specificContext.MyProperty;
}
}
公共类特定上下文:IContext
{
公共int MyProperty{get;set;}=42;
}

SpecificContext
中的
GetValueFromSpecificContext(BaseContext my)
不表示类
SpecificContext
。但它只表示与通用泛型声明中的
T
相同的泛型类型。调用该方法时会提供该方法的实际类型。..as
obj.GetValueFromSpecificContext(
如果
GetValueFromSpecificContext
T
将仅为
SpecificContext
SpecificContext
的任何派生类,则需要具有如下方法..
int GetValueFromSpecificContext(BaseContext my)其中T:SpecificContext;
通过这种方式,您可以访问方法体中
my
my
属性。@ChetanRanpariya那么第二种方法为什么有效?很抱歉在发布后更改代码,但我可以简化很多。在第二种方法中,当您执行
IsrvthatCanBegeneric
时,您声明
SrvThatCantBeGeneric
类将为类型
SpecificContext
实现接口,该类型在
GetValueFromSpecificContext(BaseContext my)中不是
T
SpecificContext
不表示类
SpecificContext
。但它只表示与通用泛型声明中的
T
相同的泛型类型。调用该方法时会提供该方法的实际类型。..as
obj.GetValueFromSpecificContext(
如果
GetValueFromSpecificContext
T
将仅为
SpecificContext
SpecificContext
的任何派生类,则需要具有如下方法..
int GetValueFromSpecificContext(BaseContext my)其中T:SpecificContext;
通过这种方式,您可以访问方法体中
my
my
属性。@ChetanRanpariya那么第二种方法为什么有效?很抱歉在发布后更改代码,但我可以简化很多。在第二种方法中,当您执行
IsrvthatCanBegeneric
时,您声明
SrvThatCantBeGeneric
类将实现类型
SpecificContext
的接口,该接口不是
T
的特定类型。接口的每个实现将具有完全不同的上下文。因此使用约束将不起作用。我需要寻找不同的方法来避免此问题。@Icen ANSWERY updated。若要访问MyP如果愿意,您需要在基类或任何接口上进行反向操作。但OOP模式保持不变。我的目标是创建强类型的步骤(函数)列表这可以从ICO容器中获取。解决流程中的动态信号流问题是一个非常好的主意。所以我喜欢IStep1:ISrvThatCantBeGeneric和IStep2:ISrvThatCantBeGeneric,然后IOC根据调用方的类型重新向我提供了每个步骤的正确实现。除了您不能访问上下文对象之外,所有操作都正常你不知道它的类型。@Icen很抱歉,我什么都不懂:我不知道你在这里用这个词汇描述的主题。但是使用像exposed这样的接口似乎可以解决你的问题,
public interface ISrvThatCantBeGeneric<T> where T : SpecificContext
{
  int GetValueFromSpecificContext(T specificContext);
}

public class SrvThatCantBeGeneric<T> : ISrvThatCantBeGeneric<T> where T : SpecificContext
{
  public int GetValueFromSpecificContext(T specificContext)
  {
    return specificContext.MyProperty;
  }
}

public class SpecificContext
{
  public int MyProperty { get; set; } = 42;
}

public class SpecificContextChild : SpecificContext
{

  public SpecificContextChild()
  {
    MyProperty = 10;
  }
}

var server = new SrvThatCantBeGeneric<SpecificContextChild>();
var context = new SpecificContextChild();
Console.WriteLine(server.GetValueFromSpecificContext(context));
public interface IContext
{
  int MyProperty { get; set; }
}
public interface ISrvThatCantBeGeneric
{
  int GetValueFromSpecificContext<T>(T specificContext) where T : IContext;
}

public class SrvThatCantBeGeneric : ISrvThatCantBeGeneric
{
  public int GetValueFromSpecificContext<T>(T specificContext) where T : IContext
  {
    return specificContext.MyProperty;
  }
}

public class SpecificContext : IContext
{
  public int MyProperty { get; set; } = 42;
}
public interface ISrvThatCantBeGeneric<T> where T : IContext
{
  int GetValueFromSpecificContext(T specificContext);
}

public class SrvThatCantBeGeneric<T> : ISrvThatCantBeGeneric<T> where T : IContext
{
  public int GetValueFromSpecificContext(T specificContext)
  {
    return specificContext.MyProperty;
  }
}

public class SpecificContext : IContext
{
  public int MyProperty { get; set; } = 42;
}