是否可以创建一个未指定方法的返回类型的接口?

时间:2017-12-06 09:55:19

标签: c# generics interface

我发现很难用英语描述这个,因为它不是我的第一语言,所以我希望问题/标题有意义。

我需要知道是否可以使用可以在实现中返回任何类型的方法创建接口。

我想制作一个可以注入另一个类的通用映射器。但我想知道我是否可以制作通用映射器,或者我是否需要为不同类型制作不同的接口。对我来说,制作相同但只有不同返回类型的不同接口看起来毫无意义吗?

有点像这样:

public interface IMapper()
{
    T MapObject() where T = the return type
    List<T> MapList() where T = the return type;
}

public class OrderMapper : IMapper()
{
   OrderDetails MapObject()
   {
      mapping code...
      return OrderDetails;
   }
}

public class CustomerMapper : IMapper()
{
    CustomerDetails MapObject()
    {
        mapping code...
        return CustomerDetails;
    }
}

这可能吗?或者我只是迟钝,我需要制作不同的界面。

1 个答案:

答案 0 :(得分:5)

Generics正是您所需要的:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
ext {
    compileSdkVersion = 25
    minSDKVersion = 18
    targetSDKVersion = 25
    buildToolsVersion = "26.0.2"
    supportLibVersion = "25.3.1"
    playServicesVersion = "11.0.2"
    digitsVersion = "1.10.3"
    crashlyticsVersion = "2.8.0"
    facebookStethoVersion = "1.5.0"
    facebookSDKVersion = "4.7.0"
    jUnitVersion = "4.12"
    squareUpOkHttpVersion = "3.9.1"
    squareUpRetrofitVersion = "2.3.0"
    squareUpPicassoVersion = "2.5.2"
    eventBusVersion = "3.1.1"
    commonsIOVersion = "2.5"
    commonsLangVersion = "3.7"
    ThinDownloadManagerVersion = "1.3.0"
    exoplayerVersion = "r1.5.8"
    constraintLayoutVersion = "1.0.2"
    leakCanaryVersion = "1.5.1"
    textDrawableVersion = '1.0.1'
    badgeViewVersion = '1.1.0'
    zxingVersion = '3.5.0'
    multidexVersion = '1.0.2'
    androidTagViewVersion = '1.1.4'
    ical4jVersion = '2.0.5'
    calendarViewVersion = '1.0.4'
    commonsCodecVersion = '1.10'
    concurrentUtilVersion = '3.1'
    AndroidAudioRecorderVersion = '0.3.0'
    objectboxVersion = '1.3.3'
}
buildscript {
    repositories {
        maven { url "http://objectbox.net/beta-repo/" }
        jcenter()
        mavenCentral()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
//        classpath 'com.google.gms:google-services:1.5.0'
        classpath 'com.google.gms:google-services:3.1.0'
//        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.3"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        jcenter()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
        maven { url "http://objectbox.net/beta-repo/" }
    }
}


dependencies {
}