我使用以下代码比较类型,以便DataContractSerializer在必要时使用正确的类型重新初始化.
private void InitializeSerializer(Type type)
{
if (this.serializer == null)
{
this.serializer = new DataContractSerializer(type);
this.typeToSerialize = type;
}
else
{
if (this.typeToSerialize != null)
{
if (this.typeToSerialize.GetType() != type.GetType())
{
this.serializer = new DataContractSerializer(type);
this.typeToSerialize = type;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,当我比较两种类型时,结果始终为真,我从不输入最终的'if'语句并重新初始化我的序列化器.
我可以在比较中设置一个断点,并清楚地看到这两种类型是
List<Host>(this.typeToSerialize.GetType())和
Post(type.GetType())
Host和Post共享一个共同的祖先,但这不应该影响结果.
我在读虽然关于在计算器上不纯的方法问题,在这里,它让我想起了结构设计的最佳实践.
阅读有关创建不可变结构的示例,此处属性仅定义为getter.
public DateTime Start { get { return start; } }
public DateTime End { get { return end; } }
public bool HasValue { get { return hasValue; } }
Run Code Online (Sandbox Code Playgroud)
其他地方的其他示例包括System.Drawing.Point属性中的getter和setter.
public int Y {
get {
return y;
}
set {
y = value;
}
}
Run Code Online (Sandbox Code Playgroud)
该设计准则不指定,但他们都相当简洁.结构属性的推荐方法是什么?只读或允许写作?
我使用 nuget for c# 下载了 ImageProcessor 库。我正在使用它来上传网站的图像并调整其大小。上传过程工作正常,除了当我尝试查看上传的图像时,它看起来从原始图像向后旋转了 90 度。这是我正在使用的代码:
ISupportedImageFormat format = new JpegFormat { Quality = 70 };
using (MemoryStream inStream = new MemoryStream(_img))
{
using (MemoryStream outStream = new MemoryStream())
{
// Initialize the ImageFactory using the overload to preserve EXIF metadata.
using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false))
{
// Load, resize, set the format and quality and save an image.
imageFactory.Load(inStream)
.Resize(new ResizeLayer(new Size(width, height), resizeMode: resizeMode))
.Format(format)
.Save(outStream);
}
return outStream.ToArray();
}
}
Run Code Online (Sandbox Code Playgroud) c# image-processing image-uploading asp.net-mvc-4 imageprocessor
我注意到如果我使用Datacontractserializer将一个对象保存回文件,如果新xml的长度比文件中最初存在的xml短,那么原始xml的残余与新xml的长度将保持在一起该文件将打破xml.
有没有人有解决这个问题的好方法?
这是我用来持久保存对象的代码:
/// <summary>
/// Flushes the current instance of the given type to the datastore.
/// </summary>
private void Flush()
{
try
{
string directory = Path.GetDirectoryName(this.fileName);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
FileStream stream = null;
try
{
stream = new FileStream(this.fileName, FileMode.OpenOrCreate);
for (int i = 0; i < 3; i++)
{
try
{
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, new System.Text.UTF8Encoding(false)))
{
stream = null;
// The serializer is initialized upstream.
this.serializer.WriteObject(writer, this.objectValue);
}
break;
}
catch …Run Code Online (Sandbox Code Playgroud) 我有一天......
这是我的班级:
/// <summary>
/// Represent a trimmed down version of the farms object for
/// presenting in lists.
/// </summary>
public class PagedFarm
{
/// <summary>
/// Gets or sets Name.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets Slug.
/// </summary>
public string Slug { get; set; }
/// <summary>
/// Gets or sets Rating.
/// </summary>
public int Rating { get; set; }
/// <summary>
/// Gets or sets City. …Run Code Online (Sandbox Code Playgroud) 使用我的webapp,我将使用散列生成的文件名将缓存的文件存储在各个子目录中,以优化性能水平。我知道可以提高性能的一种方法是,确保生成的名称遵循8.3文件名结构,这样NTFS不必生成短文件名(我将无法在注册表中进行设置)。
为了做到这一点,尽管我必须将哈希(我在想SHA1)修剪为8个字符,但是显然这将大大增加冲突的可能性。我想知道碰撞的可能性是多少?
我在这里看到了完整的SHA1哈希冲突率的答案,但是我的数学很糟糕,因此计算值远远超出了我的范围。
我正在使用gatsby创建一个简单的博客。当我尝试搜索特定图像时,出现来自graphql的错误。我有以下配置:
已安装 "gatsby-image": "^1.0.55"
graphql`
query MainLayoutQuery {
heroImage: imageSharp(id: { regex: "/hero.jpg/" }) {
id
sizes(quality: 100) {
base64
tracedSVG
aspectRatio
src
srcSet
srcWebp
srcSetWebp
sizes
originalImg
originalName
}
}
}
`
Run Code Online (Sandbox Code Playgroud)
当我在graphql ui中运行该查询时,我得到:
{
"errors": [
{
"message": "Cannot read property 'id' of undefined",
"locations": [
{
"line": 31,
"column": 3
}
],
"path": [
"heroImage"
]
}
],
"data": {
"heroImage": null
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我在不使用正则表达式的情况下运行相同的查询,则效果很好:
{
heroImage: imageSharp {
id
sizes(quality: 100) {
base64
tracedSVG
aspectRatio
src …Run Code Online (Sandbox Code Playgroud) 我正在尝试定义一种方法,我可以控制对工作中使用的javascript方法的访问(编码标准改进驱动的一部分).
我计划使用的模式很好,直到我想到我们使用第三方脚本方法做什么.
如何调整我的代码以允许从jQuery方法内部访问私有函数?
var NameSpace = new function () {
// My private function I want to access.
var privateFunction = function () {
};
this.publicFunction = function () {
// I can access my private function here.
privateFunction();
jQuery(window).resize(function () {
// But not here :(
privateFunction();
});
};
};
Run Code Online (Sandbox Code Playgroud) 我正在浏览twitter bootstrap的代码,并且我已经在这个代码片段中进行了几次昏迷.
href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
Run Code Online (Sandbox Code Playgroud)
正则表达式是我的盲点,我无法弄清楚其中的大多数.它取代了什么?在#之后是空白吗?
边注.任何人都可以为正则表达式的学费推荐一个好的来源吗?
c# ×4
javascript ×2
regex ×2
.net ×1
c#-4.0 ×1
comparison ×1
gatsby ×1
graphql ×1
linq ×1
reflection ×1
struct ×1
types ×1