سؤال

لدي هذه الواجهة

public interface TestInterface
{
   [returntype] MethodHere();
}

public class test1 : TestInterface
{
   string MethodHere(){
      return "Bla";
   }
}

public class test2 : TestInterface
{
   int MethodHere(){
     return 2;
   }
}

هل هناك أي طريقة لجعل [returntype] ديناميكية؟

هل كانت مفيدة؟

المحلول

إما أن تعلن نوع الإرجاع ككائن ، أو استخدم واجهة عامة:

public interface TestInterface<T> {
    T MethodHere();
}

public class test3 : TestInterface<int> {
   int MethodHere() {
      return 2;
   }
}

نصائح أخرى

ليس صحيحا متحرك لكن يمكنك جعلها عامة:

public interface TestInterface<T>
{
    T MethodHere();
}

public class Test1 : TestInterface<string>
... // body as before
public class Test2 : TestInterface<int>
... // body as before

إذا لم يكن هذا ما تتبعه ، فيرجى إعطاء المزيد من التفاصيل حول كيفية تمكنك من أن تكون قادرًا على ذلك استعمال الواجهة.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top