Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在不同的、不相关的类中调用同名的方法?_C#_Unity3d - Fatal编程技术网

C# 如何在不同的、不相关的类中调用同名的方法?

C# 如何在不同的、不相关的类中调用同名的方法?,c#,unity3d,C#,Unity3d,我正在统一编写c#脚本 public class something1 : MonoBehaviour { public void Dosomething(){ } } public class something2 : MonoBehaviour { public void Dosomething(){ } } public class callingDoSometing { public void callALLDosomething(){

我正在统一编写c#脚本

public class something1 : MonoBehaviour
{
    public void Dosomething(){

    }
}

public class something2 : MonoBehaviour
{
    public void Dosomething(){

    }
}

public class callingDoSometing
{
    public void callALLDosomething(){
        something1 s1 = new something1();
        something2 s2 = new something2();
        something3 s3 = new something3();
        something4 s4 = new something4();
        .
        .
        .
        s1.Dosomething();
        s2.Dosomething();
        s3.Dosomething();
        s4.Dosomething();
        .
        .
        .
    }
}
我需要从不同的类调用具有相同名称的方法(这里是
Dosomething()
)。
实际上,我在其他OOP语言中需要类似于多重继承的东西,我不想创建其他语言。

在C#中通常这样做的方式是声明一个接口;可以实现任意多个接口,但只能扩展一个基类型

interface IDoSomething
{
  void DoSomething();
}
public class Thing1 : SomeBaseClass, IDoSomething
{
    public void DoSomething(){ }
}
public class Thing2 : SomeBaseClass, IDoSomething
{
    public void DoSomething() { }
}
现在您可以使用
IDoSomething
作为一种类型:

IDoSomething i1 = new Thing1();
IDoSomething i2 = new Thing2();
i1.DoSomething();
i2.DoSomething();

您希望对两个没有共同父项的
单一行为
执行此操作的事实可能表明存在一些可疑的工程问题,但使用以下方法完全可以做到这一点


通常这是通过接口实现的

public interface IDoSomething
{
    void Dosomething();
}
public class something1 : MonoBehaviour, IDoSomething
{
    public void Dosomething() { }
}
public class something2 : MonoBehaviour, IDoSomething
{
    public void Dosomething() { }
}
如果您试图动态发现这些类,您可以执行以下操作:

public class CallingDoSomething
{
    private Type[] Get_IDoSomethingTypes()
    {
        var allTypes = typeof(CallingDoSomething).Assembly.GetTypes();
        var searchFor = typeof(IDoSomething);

        return allTypes.Where(x => searchFor.IsAssignableFrom(x))
                       .Where(x => x.IsClass)
                       .ToArray();
    }
    public void CallAllSomethings()
    {
        var types = Get_IDoSomethingTypes();
        foreach (var type in types)
        {
            var instance = (IDoSomething)Activator.CreateInstance(type);
            instance.Dosomething();
        }
    }
}
如果已经实例化了这些类的集合,则始终将它们视为IDO对象:

public class CallingDoSomething
{
    private List<IDoSomething> m_somethingDoers = new List<IDoSomething>();
    public void OtherCodeThatPopulatedSomethingDoers()
    {
        // ...
    }

    public void CallAllSomethings()
    {
        foreach (var s in m_somethingDoers)
            s.Dosomething();
    }
}
公共类调用dosomething
{
私有列表m_somethingDoers=新列表();
public void othercode填充了somethingdoers()
{
// ...
}
public void CallAllSomethings()
{
foreach(m_something doers中的var s)
s、 Dosomething();
}
}

创建一个所有类都实现的公共接口?
someting1 s4=newsomething4()这里有一个类强制转换异常。无法将
something4
转换为
something1
您不应该使用
new
来实例化
单一行为。您应该改用
AddComponent
。您是否控制类
Something1
Something2
的定义?也就是说,您可以更改它们的源代码并重新编译它们,或者它们是提供给您的,而您无法更改它们?既然您正在寻找为什么不尝试常规的“使用接口”建议?请注意,在Unity中,接口也可以使用gameObject.GetComponent()作为组件检索,但只能将抽象类放入检查器中。如果需要将这些单行为拖放到inspector中,可以使用类似的抽象类方法。谢谢您回答我的问题。但我还有一个。如果我不知道类或对象是否有DoSomething(),有没有办法按任意顺序调用所有DoSomething()。@linesra:同样,听起来你在问XY问题。你到底有什么问题?谢谢你的回答。这是最接近于理解和回答的问题(是的,我无法将我的想法清晰地转化为问题)。但是如何为具有确切类型的实例“foreach”?
public class CallingDoSomething
{
    private Type[] Get_IDoSomethingTypes()
    {
        var allTypes = typeof(CallingDoSomething).Assembly.GetTypes();
        var searchFor = typeof(IDoSomething);

        return allTypes.Where(x => searchFor.IsAssignableFrom(x))
                       .Where(x => x.IsClass)
                       .ToArray();
    }
    public void CallAllSomethings()
    {
        var types = Get_IDoSomethingTypes();
        foreach (var type in types)
        {
            var instance = (IDoSomething)Activator.CreateInstance(type);
            instance.Dosomething();
        }
    }
}
public class CallingDoSomething
{
    private List<IDoSomething> m_somethingDoers = new List<IDoSomething>();
    public void OtherCodeThatPopulatedSomethingDoers()
    {
        // ...
    }

    public void CallAllSomethings()
    {
        foreach (var s in m_somethingDoers)
            s.Dosomething();
    }
}