我想在运行时动态地将MEF Export属性应用于某个类型,就好像该类型在编译时应用了Export属性一样.
有一个简单的方法吗?
除此之外,是否有一种复杂的方法可以做到这一点?
我见过的每个例子都JSON_MODIFY显示了将一个简单的值(如a)string插入到数组中.
假设我的SQL Server列中存储了以下JSON:
[{"id": 1, "name": "One"}, {"id": 2, "name": "Two"}]
我如何追加{"id": 3, "name": "Three"}它?
当我尝试使用JSON_MODIFY如下所示时,string插入a:
UPDATE TheTable SET TheJSON = JSON_MODIFY(TheJSON, 'append $', N'{"id": 3, "name": "Three"}') WHERE Condition = 1;
以下是TheJSON列的结果值:
[{"id": 1, "name": "One"}, {"id": 2, "name": "Two"}, "{\"id\":3, \"name\": \"Three\"}"]
我注意到我可以像这样创建我想要的JSON字符串:
SELECT json.*
FROM TheTable t
CROSS APPLY OPENJSON(t.TheJSON) WITH (
id int N'$.id',
name nvarchar(100) N'$.name'
)
UNION ALL
SELECT 3 as …Run Code Online (Sandbox Code Playgroud) 我的C#类必须能够处理通过tcp流式套接字连接接收的大量事件.类的套接字从tcp服务器接收的事件消息量是完全可变的.例如,有时它只会在十秒钟内收到一条事件消息,而有时它会在一秒钟内收到六十条事件消息.
我正在使用Socket.ReceiveAsync来接收消息.如果接收操作处于挂起状态,则ReceiveAsync返回true;如果线路上已存在数据且接收操作同步完成,则ReceiveAsync返回false.如果操作处于挂起状态,Socket将在IO完成线程上调用我的回调,否则我在当前(IOC)线程中调用我自己的回调.此外,与事件消息混合我还收到对发送到此tcp服务器的命令的响应.响应消息立即处理; 单独地,通过解雇线程池工人.
但是,我想将事件消息排队,直到我有足够的(N)或者直到线上没有更多...然后激活线程池工作者来处理一批事件消息.此外,我希望所有事件都按顺序处理,所以我只希望一个线程池工作者一次处理这个事件.
事件消息的处理器只需要将消息缓冲区复制到对象中,引发事件然后将消息缓冲区释放回环形缓冲池.所以我的问题是......你认为实现这一目标的最佳策略是什么?
你需要更多信息吗?让我知道.谢谢!!
如果在IIS7或WPAS中托管我的WCF服务,是否可以将两个或多个服务加载到同一个AppDomain中,以便它们可以共享静态变量?
我正在查看我的代码,我发现了一些扩展方法,我为了从System.Collections.Generic.Stack中删除项目而编写.我很好奇,所以我看了Stack with Reflector的来源,我可以看到他们将它实现为数组而不是链表,我只是想知道为什么?使用链表,不需要调整内部数组的大小......
以下是我的扩展,欢迎任何批评或建议.谢谢.
public static Stack<T> Remove<T>(this Stack<T> stack, T item)
{
Stack<T> newStack = new Stack<T>();
EqualityComparer<T> eqc = EqualityComparer<T>.Default;
foreach( T newItem in stack.Reverse() )
{
if( !eqc.Equals(newItem, item) )
{
newStack.Push(newItem);
}
}
return newStack;
}
/// <summary>
/// Returns a new Stack{T} with one or more items removed, based on the given predicate.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="stack"></param>
/// <param name="fnRemove"></param>
/// <returns>The new stack.</returns>
/// <remarks>
/// We have to turn …Run Code Online (Sandbox Code Playgroud) 当我输入一个文字整数后跟.toString()进入Node时,它通过响应来输入一个新的范围....
> 3.toString()
...
Run Code Online (Sandbox Code Playgroud)
> 'foo:' + 3.toString()
...
Run Code Online (Sandbox Code Playgroud)
> 'foo:' + 3.toString() + ':bar'
...
Run Code Online (Sandbox Code Playgroud)
> true.toString()
'true'
Run Code Online (Sandbox Code Playgroud)
即便如此!
> 10.50.toString()
'10.5'
Run Code Online (Sandbox Code Playgroud)
在括号中包装文字整数有效:
> (3).toString()
'3'
Run Code Online (Sandbox Code Playgroud)
有这个原因还是你认为这是一个错误?
标题主要是说.我想在C#中向基类Dictionary类添加一个简单的扩展方法.起初我将其命名为Pop(TKey键),有点像Stack方法,但它接受用于查找的键.
然后我要做Take(TKey键),但它与同名的LINQ方法一致......虽然C#3.0允许你这样做,但我不喜欢它.
所以,您如何看待,只要坚持使用Pop,或者"找到并删除元素"有更好的术语吗?
(我觉得有点不好意思问这个问题,这似乎是一件小事......但我喜欢使用标准,而且我对很多语言/环境没有广泛的经验.)
编辑:对不起,应该解释更多....在这个例子中,我不能使用术语删除,因为它已经由我用新方法扩展的类定义.
编辑2:好的,这是我迄今为止所拥有的,受到人群智慧的启发:
public static TValue Extract<TKey, TValue>
(
this Dictionary<TKey, TValue> dict,
TKey key
)
{
TValue value = dict[key];
dict.Remove(key);
return value;
}
public static bool TryExtract<TKey, TValue>
(
this Dictionary<TKey, TValue> dict,
TKey key,
out TValue value
)
{
if( !dict.TryGetValue(key, out value) )
{
return false;
}
dict.Remove(key);
return true;
}
Run Code Online (Sandbox Code Playgroud) naming programming-languages terminology naming-conventions data-structures
我正在为客户端/服务器应用程序中的"客户端"类实现异步命令模式.我以前做过一些套接字编码,我喜欢他们在Socket/SocketAsyncEventArgs类中使用的新异步模式.
我的异步方法如下所示:public bool ExecuteAsync(Command cmd);如果执行挂起则返回true,如果同步完成则返回false.我的问题是:即使出现异常,我是否应该始终调用回调(cmd.OnCompleted)?或者我应该从ExecuteAsync中抛出异常吗?
如果您需要,可以在这里找到更多细节.这类似于使用SocketAsyncEventArgs,但是我的类被称为SomeCmd而不是SocketAsyncEventArgs.
SomeCmd cmd = new SomeCmd(23, 14, 10, "hike!");
cmd.OnCompleted += this.SomeCmd_OnCompleted;
this.ConnectionToServer.ExecuteAsync(cmd);
Run Code Online (Sandbox Code Playgroud)
与Socket类一样,如果需要与回调方法(在本例中为SomeCmd_OnCompleted)进行协调,则可以使用ExecuteAsync的返回值来了解操作是否挂起(true)或操作是否同步完成.
SomeCmd cmd = new SomeCmd(23, 14, 10, "hike!");
cmd.OnCompleted += this.SomeCmd_OnCompleted;
if( this.ConnectionToServer.ExecuteAsync(cmd) )
{
Monitor.Wait( this.WillBePulsedBy_SomeCmd_OnCompleted );
}
Run Code Online (Sandbox Code Playgroud)
这是我的基类的大大简化版本,但您可以看到它的工作原理:
class Connection
{
public bool ExecuteAsync(Command cmd)
{
/// CONSIDER: If you don't catch every exception here
/// then every caller of this method must have 2 sets of
/// exception handling:
/// One in the handler of …Run Code Online (Sandbox Code Playgroud) 我不确定"先测试"是如何工作的,我想听听有关何时以及为何采用这种方法的争论.
我听说在编写单行实现之前,经常建议编写测试和模拟.但是,我不禁想到它并不适合所有情况.例如,假设我正在制作原型,我不确定一切是如何运作的.所以我开始找到我认为需要的每个步骤的示例并将它们放入我的代码中.最后,我有一个我的理论的证据,并没有花那么长时间.这基本上是"我的考验".这不是一个单元测试,但它是一个测试(很可能是一个控制台应用程序).
这几乎就是我的工作方式.我想想我想做什么并尝试去做.如果它工作,那么我最终回去编写单元测试,以便我可以陷阱回归.这与你"应该做的"不同吗?
下面的_otherThing字段是否会被锁保护?
class ThreadSafeThing
{
private readonly object _sync = new object();
private SomeOtherThing _otherThing;
public SomeOtherThing OtherThing { get { lock(_sync) return _otherThing; } }
public void UpdateOtherThing(SomeOtherThing otherThing)
{
lock(_sync) _otherThing = otherThing;
}
}
Run Code Online (Sandbox Code Playgroud) 我想创建一个index.d.ts文件,该文件引用来自称为的流行NPM包中的某些类型redux。
(在我index.d.ts文件的顶部)
/// <reference path="../../../node_modules/redux/index.d.ts" />这行不通。我怀疑这是因为redux/index.d.ts文件根本没有声明名称空间。当我编辑redux/index.d.ts文件并用包裹所有内容时declare namespace Redux { ... },一切正常。我在redux@^4.0.0。
/// <reference types="redux" />除非我安装了NPM软件包@types/redux/@3.6.31,否则它将无法工作,该软件包的类型过时且与发布的类型不匹配。最新版本的@types/redux软件包只是一个已弃用的占位符,该占位符表示要使用正式的redux软件包类型(不起作用)。
import { Middleware } from "redux";当我将import上面显示的语句放入index.d.ts文件中时,文件中的每个声明都中断了(即,我依赖于自己的index.d.ts文件进行类型定义的每个其他位置都停止从中读取任何类型。)文件import类型的语句d.ts使Typescript将该文件视为模块,而不是定义文件。
@types/redux@3.6.31而不是最新的不推荐使用的占位符版本@types/redux。node_modules/redux/index.d.ts以将所有内容包装在名称空间中,例如declare namespace Redux { ... }。我的项目使用create-react-appv2作为JavaScript项目进行了引导。我只是使用d.ts文件来记录一些接口。我的编辑器是VSCode最新/稳定的。
javascript intellisense typescript visual-studio-code typescript-typings
因此,根据MSDN,System.Threading.Timer在ThreadPool线程上调用它的回调委托.有没有办法让它使用自定义ThreadPool?(例如,Jon Skeet的CustomThreadPool.)
我正在使用承载令牌为OAuth 2配置AspNet.Identity,我已经看到了实现该OAuthAuthorizationServerProvider.GrantRefreshToken方法的多个示例,其中作者演示了new ClaimsIdentity如下所示添加声明的能力.
我试图在我的单服务器(即我的Web API项目是授权+资源服务器)的上下文中理解这一点,如果需要,我可以在以后将其拆分为单独的服务器.
public override Task GrantRefreshToken(OAuthGrantRefreshTokenContext context)
{
var originalClient = context.Ticket.Properties.Dictionary["as:client_id"];
var currentClient = context.ClientId;
if (originalClient != currentClient)
{
context.SetError("invalid_clientId", "Refresh token is issued to a different clientId.");
return Task.FromResult<object>(null);
}
// Change auth ticket for refresh token requests
var newIdentity = new ClaimsIdentity(context.Ticket.Identity);
// CONSIDER: I don't know why you would add a claim here, but here's an example.
//var newClaim = newIdentity.Claims.Where(c => c.Type == "newClaim").FirstOrDefault();
//if (newClaim != …Run Code Online (Sandbox Code Playgroud) .net ×5
c# ×4
javascript ×2
syntax ×2
asp.net ×1
composition ×1
iis-7 ×1
intellisense ×1
json ×1
locking ×1
mef ×1
methodology ×1
naming ×1
networking ×1
node.js ×1
oauth ×1
oauth-2.0 ×1
sockets ×1
sql ×1
sql-server ×1
stream ×1
t-sql ×1
tcp ×1
terminology ×1
testing ×1
typescript ×1
unit-testing ×1
wcf ×1
web-services ×1