我很好奇是否还有其他方法可以在 C# 8 中使用新的 switch 表达式编写类似的内容?
public static object Convert(string str, Type type) =>
type switch
{
_ when type == typeof(string) => str,
_ when type == typeof(string[]) => str.Split(new[] { ',', ';' }),
_ => TypeDescriptor.GetConverter(type).ConvertFromString(str)
};
Run Code Online (Sandbox Code Playgroud)
因为_ when type == typeof(string)看起来有点奇怪,尤其是当我们有打字模式和其他非常方便的工具时。
当我尝试使用以下代码动态导入 vue 组件时:
const components = require.context('./', true, '/^index\.js$/');
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
app.js:9 Uncaught TypeError: __webpack_require__(...).context is not a function
at Module../asset/app.js (app.js:9)
at __webpack_require__ (bootstrap:782)
at fn (bootstrap:150)
at Object.0 (app.293d5fe1d8a073fed37a.bundle.js:1747)
at __webpack_require__ (bootstrap:782)
at checkDeferredModules (bootstrap:45)
at bootstrap:858
at bootstrap:858
Run Code Online (Sandbox Code Playgroud)
这是为什么?如何解决?我错过了什么?