示例代碼: is a way to use packages that were not designed for that framework. Basically you tell it "Use those targets even though they don't seem to be s ...
示例代碼:
"frameworks": {
"netcoreapp1.0.0": { "imports" : "portable-net45+win81" }
}
imports
is a way to use packages that were not designed for that framework. Basically you tell it "Use those targets even though they don't seem to be supported. I know what I'm doing".
註:摘錄自 Frameworks and imports sections in project.json: what are they?
上面這段解釋什麼意思?字面意思:imports
是使用程式包的一種方式,雖然不是為這種框架設計的,基本上你告訴它“使用這些目標框架,即使它們似乎不被支持,但我知道我在做什麼”,翻譯的比較生硬,你可以這樣理解:imports
的作用是各個平臺之間的相容處理,並且針對的是不支持的程式包。
概念理解還是有些問題,再來看下麵的解釋。
摘錄:What it says right here is that I’m targeting the netcoreapp1.0 framework (which is the default framework for a .NET Core application). With the “imports” bit I’m also stating that any dependency I want to include that targets dnxcore50 should be treated as compatible with netcoreapp1.0. Basically I’m instructing the tooling to ignore the fact that dnxcore50 isn’t the same thing as netcoreapp1.0 because I know it is the same thing (or more precisely Microsoft knows that they are the same).
翻譯:需要說明的是,如果目標框架平臺是 netcoreapp1.0,使用imports dnxcore50
,就可以使 netcoreapp1.0 和 dnxcore50 平臺相容,並且使工具忽略 netcoreapp1.0 和 dnxcore50 不一樣的事實,因為我知道它是相同的事情(或更確切的 Microsoft 知道他們是相同的)。
解釋:舉一個示例,比如 WebApi.Client 程式包,分別實現了 netcoreapp1.0 和 net451 平臺版本(代碼完全一樣,只是基於的平臺不一樣),然後有一個應用程式是 netcoreapp1.0 平臺的,然後它去引用 net451 平臺的 WebApi.Client 程式包,在 VS2015 中會報不支持平臺錯誤,雖然兩種平臺版本的代碼完全一樣,但還是不能跨平臺飲用,imports
就是解決這個問題的,並且使 VS2015 忽略這個錯誤。
摘錄:A word of warning is in order here though. Theoretically I could “import” any framework that’s currently out there I can install pretty much any package into my project. However, that doesn’t mean that my application will actually run and work. If a package I installed into my project is using an API that’s not available on the platform that my application will be running on it will simply fail at runtime.
Therefore you should be careful when using “imports” and make sure that you test your application properly on the actual framework it will be running on. Think of it as when you’re using “imports” you’ve turned off the tooling and you are on your own and its your job to make sure that your application works correctly.
翻譯:警告在這裡,理論上,我可以imports
任何框架平臺,然後安裝所有平臺的程式包。然而,這並不意味著我的應用程式可以實際運行和工作。如果我安裝到我項目的程式包,在這個平臺上 API 不可用,我的應用程式在運行時將是失敗的。
因此,在使用imports
時應該小心,要確保在平臺框架上,應用程式可以實際正常運行。想象下,當你使用了imports
,你就關閉了 VS2015 的提示,你就要確保它是正常工作的。
解釋:舉個例子,比如 WebApi.Client 程式包,分別實現了 netcoreapp1.0 和 net451 平臺版本(代碼完全不一樣,內部實現使用了不同平臺的 API),然後有一個應用程式是 netcoreapp1.0 平臺的,然後它去引用 net451 平臺的 WebApi.Client 程式包,需要使用imports net451
,在 VS2015 編譯完全沒什麼問題,但在 netcoreapp1.0 平臺實際運行的時候,就會出錯了,因為不同平臺的 WebApi.Client 程式包,使用了不同平臺的 API。
註:摘錄自 .NET Platform Standard and the magic of “imports”
翻譯的比較差,但大概意思是懂了,總的來說,使用imports
,只是解決了在 VS2015 中平臺不支持的錯誤提示,但並不一定說明實際是正常運行的,所以,要在實際運行環境中,測試引用其他平臺的程式包,執行是否有問題。