我有这个代码:
static class Global
{
public static readonly IChannelsData Channels = new ChannelsData();
public static readonly IMessagesData Messages = new MessagesData();
}
Run Code Online (Sandbox Code Playgroud)
我的理解是,因为这个类是静态的,所以现在给它们一个实例是不可能的Global.Channels或者Global.Messages是null.
但是,我尝试访问该属性
public class Channel : IComparable
{
...
private SortedList<string, Message> _messages;
[JsonConstructor]
public Channel()
{
_messages = new SortedList<string, Message>();
}
[OnDeserialized]
private void Init(StreamingContext context)
{
**Global.Channels.RegisterChannel(this);**
}
...
}
Run Code Online (Sandbox Code Playgroud)
我得到NullReferenceException的Global.Channels,这是我在即时窗口中确认.进一步让我感到困惑,我可以点击断点new ChannelData(),所以我知道静态成员正在填充 - 成功 - 在某些时候.
更多背景,评论请求:
private Hashtable _channels;
public ChannelsData()
{
_channels = …Run Code Online (Sandbox Code Playgroud) 我的问题的前提,用简单的英语:
Foo取决于名为的库BarFooBar依赖于Foo请考虑以下示例:
class Program
{
static void Main(string[] args)
{
Foo foo = Foo.Instance;
int id = foo.Id; // Compiler is happy
foo.DoWorkOnBar(); // Compiler is not happy
}
}
Run Code Online (Sandbox Code Playgroud)
Foo定义如下
public class Foo : Bar
{
public new static Foo Instance { get => (Foo)Bar.Instance; }
public new int Id { get => Bar.Id; }
public void DoWorkOnBar()
{
Instance.DoWork();
}
}
Run Code Online (Sandbox Code Playgroud)
条形图定义如下
public class Bar …Run Code Online (Sandbox Code Playgroud) 我一直在阅读git,我有一个非常特别的问题,我正在努力回答.
什么时候自动git gc执行?
我一直在听到各种论坛的葡萄藤,它默认发生在推送或取/拉 - 但我找不到任何验证这一点的来源.甚至文档本身也只是这个(强调我的):
一些 git命令可以自动运行git gc; 有关详细信息,请参阅下面的--auto标志
并且--auto标志指定
执行可能会创建许多松散对象的操作后,某些 git命令会运行git gc --auto.
我希望能够确定地说:
"在运行以下命令之一之前,git不会清除松散的树和blob文件:{mystery list here}.当运行其中一个命令时,如果松散对象的数量超过了值gc.auto,git将自动将对象压缩为packfile".
我需要将一个HTTP请求作为MultiPartFormData发送到REST控制器.它工作正常,但现在我在控制器上的检查声称请求的类型不正确,即使我在调试器中看到请求的类型正确.以供参考:
这是调用它的控制台应用程序代码:
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace QuickUploadTestHarness
{
class Program
{
static void Main(string[] args)
{
using (var client = new HttpClient())
using (var content = new MultipartFormDataContent())
{
// Make sure to change API address
client.BaseAddress = new Uri("http://localhost");
// Add first file content
var fileContent1 = new ByteArrayContent(File.ReadAllBytes(@"C:\<filepath>\test.txt"));
fileContent1.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "testData.txt"
};
//Add Second file content
var fileContent2 = new ByteArrayContent(File.ReadAllBytes(@"C:\<filepath>\test.txt"));
fileContent2.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{ …Run Code Online (Sandbox Code Playgroud) TL; DR
我想用C#编译
public void some_method< T >() where T : class1, class2
这可能吗?
完整的背景
除了一个参数外,我有两个相同的方法.
public SignInResponseMessage Generate(SignInRequestMessage request, (X509Certificate2 || WindowsPrincipal) principal, Uri requestUri)
{
SignInResponseMessage response = null;
ClaimsIdentity identity = null;
if (principal != null)
{
identity = CreateSubject(principal);
response = Generate(request, requestUri, identity);
}
else
{
throw new ArgumentNullException("principal");
}
return response;
}
Run Code Online (Sandbox Code Playgroud)
我正在复制这种方法,它让我内心畏缩,因为我真的想做这个DRY.环顾四周,这个文档看起来很有希望,但它只允许我添加一个类约束.我在第二节课上得到以下错误:
错误1类类型约束"class2"必须位于任何其他约束之前
如果WindowsPrincipal并且X509Certificate2是我编写的两个类,我可以轻松地使它们实现相同的界面,我会很高兴,但这不是一个选项.
有没有办法完成我想做的事情?
如果没有,我想更多地了解使这不可能的潜在机制.
在此处找到演示此问题的完整示例应用.
我正在尝试使用OWIN中间件来填充WebForms页面可以访问的静态标识符,但我注意到某些类型的行为不直观 - 特别是AsyncLocal<T>.
考虑以下工作示例,使用int:
简单的静态容器
public static class Container
{
public static int CallId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
简单的OWIN配置
[assembly: OwinStartup(typeof(Startup))]
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.Use((context, next) =>
{
Container.CallId = 5;
return next.Invoke();
});
}
}
Run Code Online (Sandbox Code Playgroud)
简单Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Container.CallId != 5)
{
throw new Exception("What happened to CallId");
}
}
Run Code Online (Sandbox Code Playgroud)
这按预期运行.中间件设置CallId为5,WebForms页面看到5.不抛出异常.
这是事情打破直觉的地方,如果我想使用AsyncLocal<int>我的CallId,那么WebForms页面中的值就不再可用了 …
Haskell的新手,我在这个表达式上遇到了一个简单的错误
matches !! length . count
Run Code Online (Sandbox Code Playgroud)
不能混合'!!' [infixl 9]和'.' [infixr 9]在相同的中缀表达式中
这让我感到惊讶 - 为什么这些运营商会分享优势呢?是什么阻止了Haskell的设计师建立(.)了比更高的优势(!!)?
我无法想象这只是被忽视了,所以我认为这里有一个我失踪的理由.
注意我意识到应用于(!!)由组成的函数(.)仍然会在编译时导致错误.我很好奇为什么Haskell的设计方式允许这个意外的错误消息.
我正在将XML文档转换为HTML文档.我想从源文档中删除处理指令的每个实例.
我已经弄清楚如何删除特定的处理指令,<xsl:template match="processing-instruction('processing_instruction_name')"/>但我正在处理的处理指令因文档而异.
我为processing_instruction_name尝试了*的变体,但是我的XSL引擎不断抛出解析错误.我想做什么?如果是这样,我该怎么办?
任何帮助表示赞赏!
我想制作尽可能干的代码.我正在使用的架构有各种数据:Channel,User,Process等.每个都有一个公共Id字符串属性.我目前正在通过以下方式抽象出许多业务逻辑:
using System;
using System.Collections.Generic;
using Vert.Slack;
namespace Vert.Interfaces
{
public abstract class DataSource<T> : IDataSource<T>
{
protected Dictionary<string, T> _items;
public DataSource()
{
_items = new Dictionary<string, T>();
}
public abstract bool Register(T item);
public T LookupUsing<String>(string key)
{
T item = default(T);
item = _items[key];
return item;
}
public IEnumerable<T> List(int? limit)
{
List<T> items;
if(!limit.HasValue)
{
items = new List<T>(_items.Values);
}
else
{
items = new List<T>(limit.Value);
items.AddRange(_items.Values);
}
return items;
}
public abstract T …Run Code Online (Sandbox Code Playgroud) 背景:我花了不到一个星期才把Haskell弄出来,到目前为止我很开心,但是这个让我很难受.这似乎是演示我的问题的最简单的例子:
我定义了以下类型.
data AnInteger = A Integer
instance Eq AnInteger where
a == a' = a == a'
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用它时
我得到的似乎是无限循环.这里发生了什么,我没有得到?
如何更正语法以提供所需的True输出?
我有一个简单的功能,并希望有点无意义的风格.
shout :: String -> String
shout input
| null input = []
| otherwise = (toUpper . head $ input) : (shout . tail $ input)
Run Code Online (Sandbox Code Playgroud)
我的直觉引领着我
pfShout :: String -> String
pfShout = (toUpper . head) : (shout . tail)
Run Code Online (Sandbox Code Playgroud)
这是对cons细胞的第一个论点的抱怨
无法将预期类型'String - > String'与实际类型'[[Char] - > Char]'匹配
可能的原因:'(:)'适用于太多参数
在表达式中:( toUpper.head):( pfShout.tr尾)
在'pfShout'的等式中:pfShout =(toUpper.head):( pfShout.tr尾)
并且抱怨cons细胞的第二个论点
无法将预期类型'[[Char] - > Char]'与实际类型'[Char] - > String'匹配
可能原因:'(.)'适用于太少的参数
在'(:)'的第二个参数中,即'(pfShout.tr尾)'
在表达式中:( toUpper.head):( pfShout.tr尾)
在'pfShout'的等式中:pfShout =(toUpper.head):( pfShout.tr尾)
我很清楚,我无法列出'String - > String'函数和 '[[Char] - …
考虑以下:
_securePassword = New SecureString()
For Each character As Char In password
_securePassword.AppendChar(character)
Next
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,文档似乎暗示这是填充SecureString实际信息的"最佳实践"方式:
从一个角色一次创建一个字符串时......
我的问题
为什么不包括New SecureString(password)?
对于我来说,这似乎是纯粹的锅炉板代码.