例如,我们有以下结构:
class Base
{
[pure]
public virtual bool IsValid(/*you can add some parameters here*/)
{
//body
}
}
class Child : Base
{
public override bool IsValid(/*you can add some parameters here*/)
{
//body
}
}
Run Code Online (Sandbox Code Playgroud)
你能否填写Base::IsValid()并Child::IsValid()与不同的机构,但没有与LSP的冲突?让我们想象它只是分析的方法,我们无法改变实例的状态.我们能做到吗?我对任何一个例子感兴趣.在一般情况下,我试图了解虚拟(身体)布尔方法是否是反模式.
oop design-patterns liskov-substitution-principle solid-principles
我需要分析一些扩展方法.例如Enumerable.ToList.
要分析的代码示例:
var test = @"
using System.Linq;
namespace Test
{
public class TestType
{
void TestMethod()
{
var empty = new[] {0};
var test = empty.ToList();
}
}
}";
Run Code Online (Sandbox Code Playgroud)
诊断:
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(AnalyzeSymbol, SyntaxKind.InvocationExpression);
}
private static void AnalyzeSymbol(SyntaxNodeAnalysisContext context)
{
var symbolInfo = context.SemanticModel.GetSymbolInfo(context.Node);
}
Run Code Online (Sandbox Code Playgroud)
但是symbolInfo.Symbol无效且没有任何候选人.如果我像这样更改代码示例:
var test = @"
using System.Linq;
namespace Test
{
public class TestType
{
void TestMethod()
{
var empty = new[] {0};
var test = …Run Code Online (Sandbox Code Playgroud) 例如,我有以下类:
1.
class MyClass1
{
public MyClass1 Method()
{
...
return new MyClass1();
}
}
class MyClass2
{
public MyClass2 Method()
{
...
return new MyClass2();
}
}
Run Code Online (Sandbox Code Playgroud)
这些方法具有相同的主体,这就是我想要提取代码并重复使用的原因.
2.
abstract class MyClass
{
protected void Method()
{
...
}
}
class MyClass1 : MyClass
{
public MyClass1 Method()
{
base.Method();
return new MyClass1();
}
}
class MyClass2 : MyClass
{
public MyClass2 Method()
{
base.Method();
return new MyClass2();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,由于有很多这样的方法,最好将方法移动到基类MyClass中:
3.
abstract class MyClass<T>: where T …Run Code Online (Sandbox Code Playgroud) 我试图用来firewallAPI.dll添加规则.它适用于calc.exe(或其他一些文件),如下所述,但因msdtc.exe以下异常而失败:
System.IO.FileNotFoundException:'系统找不到指定的文件.(HRESULT异常:0x80070002)'
例:
static void Main(string[] args)
{
var manager = GetFirewallManager();
if (manager.LocalPolicy.CurrentProfile.FirewallEnabled)
{
var path = @"C:\Windows\System32\calc.exe";
//var path = @"C:\Windows\System32\msdtc.exe"; // System.IO.FileNotFoundException: 'The system cannot find the file specified.
AuthorizeApplication("Test", path, NET_FW_SCOPE_.NET_FW_SCOPE_ALL, NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY);
}
}
private const string CLSID_FIREWALL_MANAGER =
"{304CE942-6E39-40D8-943A-B913C40C9CD4}";
private static NetFwTypeLib.INetFwMgr GetFirewallManager()
{
Type objectType = Type.GetTypeFromCLSID(
new Guid(CLSID_FIREWALL_MANAGER));
return Activator.CreateInstance(objectType)
as NetFwTypeLib.INetFwMgr;
}
private const string PROGID_AUTHORIZED_APPLICATION =
"HNetCfg.FwAuthorizedApplication";
public static bool AuthorizeApplication(string title, string applicationPath,
NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ …Run Code Online (Sandbox Code Playgroud) 我需要支持 T-SQL 和 SQL Server 的其他一些东西的 dockerized 数据库。当然,SQL Server 有一个容器,但它占用太多空间。所以我尝试将 SQL Server LocalDB(大约 200mb)放在一个 docker 容器中。
它具有静音安装模式,可以轻松安装。但问题是如何从容器外部访问它?(我也需要从我的本地 PC 访问它)这种情况有什么解决方案吗?现在我只有一个想法来实现一些服务来在 SQL Server LocalDB 和外部连接之间进行通信,然后将它暴露在同一个容器中的 db 附近。大家能不能给点建议?
我准备了一个 docker-compose 文件来部署带有数据库的容器:
services:
tmp-db:
image: microsoft/mssql-server-linux:latest
environment:
ACCEPT_EULA: Y
SA_PASSWORD: yourStrong(!)Password
ports:
- 1433:1433
Run Code Online (Sandbox Code Playgroud)
没关系。但现在我需要创建一个数据库并构建其结构。我需要执行一些sql命令。为了检查我是否能够做到这一点,我添加command到服务中:
services:
tmp-db:
image: microsoft/mssql-server-linux:latest
environment:
ACCEPT_EULA: Y
SA_PASSWORD: yourStrong(!)Password
command: /opt/mssql-tools/bin/sqlcmd -U sa -P yourStrong(!)Password -Q "SELECT [name] FROM sys.databases"
ports:
- 1433:1433
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
tmp-db_1 | Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
tmp-db_1 | Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: Error code 0x2749.
tmp-db_1 | Sqlcmd: Error: Microsoft ODBC …Run Code Online (Sandbox Code Playgroud) 我分析的代码中SqlCommand,看到它工作正常(见CompleteExecuteScalar方法).但我不确定我应该top 1在查询中使用.例如我有select name from Person和表Person包含很多记录.我应该修改qry select top 1 name from Person吗?或者我可以保持qry原样,它不会影响性能/内存等?我担心服务器可以在输出之前缓冲一些数据.
public static void Init()
{
//var task = GetSource1();
//var task = GetSource2();
//var task = GetSource3();
var task = MyClient();
MessageBox.Show(task.Result);
}
private static async Task<string> GetSource1()
{
var sourceTask = WebClient();
sourceTask.ConfigureAwait(false);
return await sourceTask;
}
private static async Task<string> GetSource2()
{
var sourceTask = MyClient();
sourceTask.ConfigureAwait(true);
return await sourceTask;
}
private static async Task<string> GetSource3()
{
var sourceTask = MyClient();
sourceTask.ConfigureAwait(false);
return await sourceTask;
}
private static async Task<string> WebClient()
{
return await new WebClient().DownloadStringTaskAsync("http://4pda.ru").ConfigureAwait(false);
}
private …Run Code Online (Sandbox Code Playgroud) .net c# synchronizationcontext task-parallel-library async-await
我知道DbContext的缓存并不是一个好主意.但我想做得很好.你怎么看待这种方式?
public class Context : DbContext
{
private Context()
{
}
private static WeakReference<Context> _cachedContext;
public Context Instance
{
get
{
Context context;
if (!_cachedContext.TryGetTarget(out context))
{
context = new Context();
_cachedContext.SetTarget(context);
}
return context;
}
}
}
Run Code Online (Sandbox Code Playgroud)
计划在没有IDisposable的情况下使用此代码.在客户端进行调用.除了单身(反)模式,这会导致什么问题?谢谢.
我是Java/Spring的新手.我需要从配置中读取一些值,但如果它不存在则应该失败.现在我有以下代码:
public class SomeClass {
@Value("${some.property:#{null}}")
private String someProperty;
public void someMethod() throws Exception {
if (someProperty == null) throw new AopConfigException("The property must be set");
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我需要添加额外的if块.我可以写那样的东西吗?
@Value("${some.property:#{throw new AopConfigException(\"The property must be set\")}}")
private String someProperty;
Run Code Online (Sandbox Code Playgroud)
要么
@Value("${some.property:#{throwException()}}")
private String someProperty;
private static void throwException() {
throw new AopConfigException("The property must be set");
}
Run Code Online (Sandbox Code Playgroud)
立即失败
更新:
如果我没有使用下面建议的某个默认值,那么它对我来说仍然不会失败.我没有java.lang.IllegalArgumentException:
c# ×6
.net ×4
sql-server ×3
docker ×2
async-await ×1
dbcontext ×1
generics ×1
java ×1
liskov-substitution-principle ×1
localdb ×1
oop ×1
performance ×1
roslyn ×1
spring ×1
spring-aop ×1
sqlcommand ×1
typing ×1