我对Powershell很新,我正在设置我的profile.ps1文件.我有一些托管的DLL,我经常使用它来维护整天的过程,我希望能够加载快速函数调用.所以我在ps1文件中创建了这个函数:
function LoadSomeDll
{
[System.Reflect.Assembly]::LoadFrom("c:\wherever\SomeLib.dll")
return new-object "SomeLib.SomeObject"
}
Run Code Online (Sandbox Code Playgroud)
然后,在Powershell中,我这样做:
PS > $myLibInstance = LoadSomeDll
Run Code Online (Sandbox Code Playgroud)
问题是$ myLibInstance虽然看起来像是被加载了,但它的行为方式与我预期的方式不同,或者如果我在没有该函数的情况下显式加载它的话.Say SomeLib.SomeObject有一个公共字符串属性"ConnectionString",它在构造对象时加载自身(来自注册表,yuck).
PS > $myLibInstance.ConnectionString
//Nothing returned
Run Code Online (Sandbox Code Playgroud)
但是,如果我没有这个功能就这样做,就像这样:
PS > [System.Reflect.Assembly]::LoadFrom("c:\wherever\SomeLib.dll")
PS > $myOtherLibInstance = new-object "SomeLib.SomeObject"
Run Code Online (Sandbox Code Playgroud)
我明白了:
PS > $myOtherLibInstance.ConnectionString
StringValueOfConnectionStringProperty
Run Code Online (Sandbox Code Playgroud)
为什么会这样?有没有办法可以从Powershell函数返回一个实例化的新对象?
提前致谢.
我正在使用MVC 6进行一些原型设计并遇到了困境.我们的项目架构非常简单:
数据层(实体框架6)
服务层(类库,引用数据层)
表示层(MVC 4,引用服务层,不引用数据层)
即使在阅读(并同意)Composition Root模式之后,我也试图使设计尽可能与原始设计相似.这意味着我的新MVC 6应用程序不知道我的DbContext
类型.您可以猜测当我尝试将一个服务类注入控制器时会发生什么:
Unable to resolve service for type My.Data.Tier.DbContext while attempting to activate My.Service.Tier.SomeService.
我相信我们之前的实现(我没有写它)DbContext
通过反映bin文件夹中的程序集来解决.有没有一种方法可以使用Microsoft.Extensions.DependencyInjection
内置于ASP.NET 5/MVC 6 的新命名空间来完成这个(或类似的东西)所以我可以避免从我的表示层对我的数据层进行硬引用?
更新
阅读Joe Audette的回答后,我想出了一个非常简单的扩展方法,我将其添加到服务层:
public static class ServiceCollectionExtensions
{
public static void RegisterDbContext(this IServiceCollection services)
{
services.AddScoped<My.Data.Tier.DbContext, My.Data.Tier.DbContext>();
}
}
Run Code Online (Sandbox Code Playgroud)
然后,在我的MVC应用程序的Startup.cs中:
using My.Service.Tier.ExtensionNamespace;
public void ConfigureServices(IServiceCollection services)
{
services.RegisterDbContext();
}
Run Code Online (Sandbox Code Playgroud) entity-framework dependency-injection entity-framework-6 asp.net-core-mvc asp.net-core
我正在使用Powershell来测试COM对象方法调用.由于设计/编码/一切都很差,这个COM对象方法只是在出错时挂起.我的默认本能是控制+ c,但这不起作用.我是否必须杀死Powershell以杀死挂起的COM方法调用?
提前致谢.
我是PowerShell新手,我很想能够编写脚本.我有一个文本文件,其中每一行都是文件名的一部分,没有路径或扩展名.我想要一个循环遍历文件每一行的单行(带有gc - Get-Content,对吗?),获取行的内容,构造源路径(网络驱动器和扩展是静态的),构造目标路径,然后复制每个文件.我的文件内容是这样的:
12345678
98765432
32145698
12365782
Run Code Online (Sandbox Code Playgroud)
我的源文件夹是这样的UNC路径:
\\server\share
Run Code Online (Sandbox Code Playgroud)
我的目标文件夹是:
c:\temp\files
Run Code Online (Sandbox Code Playgroud)
我想做相当于这个DOS命令,使用$_
文件的每一行作为文本:
copy \\server\share\$_.ext c:\temp\files\$_.ext
Run Code Online (Sandbox Code Playgroud)
我很确定我可以使用gc
和$_
访问文件的每一行,并且我需要使用它cp
来复制文件,但是我在构造源文件名和目标文件名时遇到了麻烦.
我正在使用一个使用派生自的集合的框架System.Collections.CollectionBase
.用户一直在抱怨性能,我觉得这些使用频繁的集合可能是问题的重要组成部分.有没有办法使用工具或探查器或在IL中我可以得到一些关于装箱/拆箱处罚的指标?我需要证据支持推动System.Collections.Generic
.我已经尝试过CLRProfiler,但往往会迷路而且不确定我应该寻找什么.
更新
到目前为止感谢您的所有意见.我知道这可能不是主要的瓶颈,但我正在寻找尽可能多的性能杀手的指标.这只是其中之一,不确定它有多大,因此寻找一种衡量它的方法.
如果我在VB.NET中创建一个新的类库项目,我可以创建子文件夹(一个C#),将WinForm对象添加到这些子文件夹,然后指定一个名称空间:
Namespace Sub1.Sub2
Public Class SomeForm
Public Sub New()
InitializeComponent()
End Sub
End Class
End Namespace
Run Code Online (Sandbox Code Playgroud)
这解决了ProjectRootNamespace.Sub1.Sub2.SomeForm
,这很好.
但是,如果我在VB.NET中创建一个新的WinForms项目,并尝试相同的事情,我在设计器中得到这个错误:
The class SomeForm can be designed, but is not the first class in the file. Visual Studio requires that designers use the first class in the file. Move the class code so that it is the first class in the file and try loading the designer again.
有没有办法在VB.NET WinForms应用程序的子命名空间中而不是在根命名空间中使用表单?
我刚刚编写了一个类,并意识到它的所有公共功能都封装在一个方法中.它没有属性,没有共享资源,并且不需要构造函数重载并处理它使用的任何内容.它看起来像这样:
public class ReportGenerator
{
public string GenerateReport(List<SomeClass> stuffToReportOn)
{
string fileName = String.Empty;
using(var reportDs = CreateDataSet(stuffToReportOn))
{
//do the stuff with the third party tool that
//creates the report.
//Construct the filename.
//Save the report.
}
return fileName;
}
private TypedDataSetRequiredByThirdPartyLib CreateDataSet(List<SomeClass> reportItems)
{
//create the dataset, calling two other private methods
//to build the tables/rows
}
}
Run Code Online (Sandbox Code Playgroud)
在我完成重构之后,我意识到这个类可能完全是静态的.我的问题是,应该吗?是否应将在一个公共方法中封装其所有功能的类设为静态?
我不确定这是否可行,但我正在尝试在每个列表项的前面设置一个带有复选框的有序列表,如下所示:
我已经找到了使用这个问题的缩进内容,但是无法将复选框显示在与...相同的行上<li>
.有没有办法做到这一点?
假设我有这个请求进入 API 路由:
/api/things?p=2&t=25&some_id=67&another_id=89
Run Code Online (Sandbox Code Playgroud)
我可以使用解构赋值将参数放入局部变量吗?我试过这个:
const [p, t, ...otherParams] = request.query;
Run Code Online (Sandbox Code Playgroud)
...但我在运行时得到“未定义不是函数”,这是一个我无法调试的奇怪错误。我真正喜欢的是使用带有默认值的解构。在我有缺陷的语法中,这将类似于:
const [p = 1, t = 25, ...otherParams] = request.query;
Run Code Online (Sandbox Code Playgroud)
这可能吗?如果是这样,我做错了什么?
powershell ×3
.net ×2
css ×2
alignment ×1
asp.net-core ×1
asp.net-mvc ×1
boxing ×1
c# ×1
com ×1
express ×1
html ×1
html-lists ×1
javascript ×1
namespaces ×1
performance ×1
profiling ×1
razor ×1
static ×1
vb.net ×1
winforms ×1