C# 如何在接口中创建可选的泛型方法?

C# 如何在接口中创建可选的泛型方法?,c#,generics,interface,abstraction,C#,Generics,Interface,Abstraction,我的问题太简单了,我有一个接口,其中包含一个方法 界面: public interface IAbstract { void DoSomething(); } 我有两个类在接口中包含相同的方法,所以我想让它们(类)实现接口 问题: 唯一的问题是在其中一个类中,此方法将正常实现,但在另一个类中,此方法将作为泛型方法实现 我的想法作为代码: public interface IAbstract { void DoSomething(); } 头等舱: 二等舱: public类二:

我的问题太简单了,我有一个
接口
,其中包含一个
方法

界面:

public interface IAbstract
{
    void DoSomething();
}
我有两个类在接口中包含相同的方法,所以我想让它们(类)实现接口

问题: 唯一的问题是在其中一个类中,此方法将正常实现,但在另一个类中,此方法将作为泛型方法实现

我的想法作为代码:

public interface IAbstract
{
    void DoSomething();
}
头等舱:

二等舱:

public类二:IAbstract
{
公共无效剂量测定法()
{
抛出新系统。NotImplementedException();
}
}

我知道这给了我一个错误,但我的问题是,有没有办法在不创建单独接口的情况下实现我的想法?

您有几个设计选项来获得您想要的

    static void Main(string[] args)
    {
        var one = new ClassOne();
        one.DoSomething(); 
        var two = new ClassTwo();
        two.DoSomething();
        two.DoSomething<int>();
        var three = new ClassThree<int>();
        three.DoSomething();
        three.DoSomething(100);
    }
static void Main(字符串[]args)
{
var one=新的ClassOne();
一、DoSomething();
var two=新的ClassTwo();
二、DoSomething();
二、DoSomething();
var三=新的类三();
三、DoSomething();
三、剂量(100);
}
代码呢

public interface IAbstract
{
    void DoSomething();
}
public interface IAbstract<in T> : IAbstract
{
    void DoSomething(T arg);
}

public class ClassOne : IAbstract
{
    public void DoSomething()
    {
        Debug.WriteLine("DoSomething()");
    }
}
public class ClassTwo : IAbstract
{
    public void DoSomething()
    {
        Debug.WriteLine("DoSomething()");
    }
    public void DoSomething<T>()
    {
        Debug.WriteLine("DoSomething<T>()");
    }
}
public class ClassThree<T> : IAbstract where T : new()         
{
    public void DoSomething()
    {
        Debug.WriteLine("DoSomething()");
    }
    public void DoSomething(T arg)
    {
        Debug.WriteLine("DoSomething(T arg)");
    }
}
公共接口IAbstract
{
无效剂量();
}
公共接口IAbstract:IAbstract
{
无效剂量测定(T arg);
}
公共一级:IAbstract
{
公共无效剂量测定法()
{
WriteLine(“DoSomething()”);
}
}
公开课二级:IAbstract
{
公共无效剂量测定法()
{
WriteLine(“DoSomething()”);
}
公共无效剂量测定法()
{
WriteLine(“DoSomething()”);
}
}
公共类class3:IAbstract其中T:new()
{
公共无效剂量测定法()
{
WriteLine(“DoSomething()”);
}
公共无效剂量测定(T参数)
{
Debug.WriteLine(“DoSomething(T arg)”);
}
}

您应该这样做:

public interface IAbstractRoot<T>
{
    void DoSomthing<TT>() where TT : T;
}

public abstract class IAbstractRoot : IAbstractRoot<object>
{
    public void DoSomthing<TT>()
    {
        DoSomthing();
    }

    public abstract void DoSomthing();
}
public class Child1 : IAbstractRoot<int>
{
    void IAbstractRoot<int>.DoSomthing<TT>()
    {
        throw new System.NotImplementedException();
    }
}

public class Child2 : IAbstractRoot
{
    public override void DoSomthing()
    {

    }
}
公共接口IAbstractRoot
{
void DoSomthing(),其中TT:T;
}
公共抽象类IAbstractRoot:IAbstractRoot
{
公共空间
{
dosomshing();
}
公开摘要无效DoSomthing();
}
孩子们应该是这样的:

public interface IAbstractRoot<T>
{
    void DoSomthing<TT>() where TT : T;
}

public abstract class IAbstractRoot : IAbstractRoot<object>
{
    public void DoSomthing<TT>()
    {
        DoSomthing();
    }

    public abstract void DoSomthing();
}
public class Child1 : IAbstractRoot<int>
{
    void IAbstractRoot<int>.DoSomthing<TT>()
    {
        throw new System.NotImplementedException();
    }
}

public class Child2 : IAbstractRoot
{
    public override void DoSomthing()
    {

    }
}
public类Child1:IAbstractRoot
{
void IAbstractRoot.DoSomthing()
{
抛出新系统。NotImplementedException();
}
}
公共类Child2:IAbstractRoot
{
公共覆盖无效DoSomthing()
{
}
}

您可以通过以下方法完成此操作。这是我一生中从未在专业领域使用过或见过的功能,但这里有一个例子

void Main()
{
    var a = new a();
    var b = new b<string>();
    
    a.DoSomething(1);
    b.DoSomething("Here is a string");
}

public interface IAbstract<in T>
{
    void DoSomething(T param1);
}

public class a : IAbstract<int>
{
    public void DoSomething(int param1)
    {
        Console.WriteLine($"I am specificly an int {param1}");
    }
}

public class b<T> : IAbstract<T>
{
    public void DoSomething(T param1)
    {
        Console.WriteLine($"I am a generic something {param1}");
    }
}
void Main()
{
var a=新的a();
var b=新的b();
a、 剂量测定法(1);
b、 DoSomething(“这是一根线”);
}
公共接口IAbstract
{
无效剂量测定(T参数1);
}
公开甲级:IAbstract
{
公共无效剂量测定(int参数1)
{
WriteLine($“我具体是一个int{param1}”);
}
}
b类公众:IAbstract
{
公共无效剂量测定(T参数1)
{
WriteLine($“我是一个泛型的东西{param1}”);
}
}

No您不能,因为界面
IAbstract
的用户无法知道它甚至需要一个通用参数。不清楚您想做什么,好像
ClassTwo
已经知道它需要是
int
为什么它需要一个泛型参数?或者你想在
ClassTwo
上声明泛型参数?也许这只是一个模型,但如果我不指出名为IAbstractRoot的抽象类,我的OCD会杀了我。亲爱的,没有其他方法了。接口的实现只能是通用的或非通用的。“不是两者都是”但我的意思是“I”前缀是接口而不是抽象类。是的,你是对的。你不应该给它命名(抽象)!从所有接口中删除“I”和“Abstract”关键字。这将是最好的。例如:将IAbstractRoot接口名称更改为->IRoot。并将IAbstractRoot类更改为AbstractRoot。