小编mus*_*ium的帖子

LibGit2Sharp获取自{Hash}以来的所有提交

是否可以使用LibGit2Sharp获取指定提交后的所有提交?

我试过以下......但它不起作用:

using ( var repo = new Repository( repositoryDirectory ) )
{
    //Create commit filter.
    var filter = new CommitFilter
    {
        SortBy = CommitSortStrategies.Topological | CommitSortStrategies.Reverse,
        Since = repo.Refs
    };

    /*Not Working
    if (shaHashOfCommit.IsNotEmpty())
        filter.Since = shaHashOfCommit;
    */

    var commits = repo.Commits.QueryBy( filter );
}
Run Code Online (Sandbox Code Playgroud)

c# git logging commit libgit2sharp

4
推荐指数
1
解决办法
1654
查看次数

SQL语句的某些部分嵌套太深c#EF 5

我从EF 5中得到以下异常:SQL语句的某些部分嵌套太深.重写查询或将其分解为较小的查询.

这是我的查询:

String username = “test”;
IEnumerable<Int32> roles;
IEnumerable<Int32> applications;

cnx.Users.Where ( it =>
( userName != null ?  it.name = = userName : true )  &&
( !roles.Any () || roles.Contains ( it.role_id ) ) &&
( ! applications.Any () || applications.Contains ( it.application_id ) ) )
               .Count ();
Run Code Online (Sandbox Code Playgroud)

用户是一个简单的表.角色和应用程序都是IEnumerable类型,可以为空.

如何更改我的查询,它将在EF 5(.Net 4.0)中有效?

c# sql linq entity-framework where

3
推荐指数
1
解决办法
5740
查看次数

Git 精选范围

我如何挑选 commitC4C5

\n\n

Git树

\n\n

我\xe2\x80\x99已经尝试过git cherry-pick C4..C5,但我只得到C4. \n我想我不\xe2\x80\x99真正理解这个范围的事情是如何工作的。

\n

git git-cherry-pick

3
推荐指数
1
解决办法
1418
查看次数

C#protubuf-net - 默认值覆盖protobuf数据的值

我需要使用protobuf序列化/反序列化类.对于我的类的一些属性,我需要定义一个默认值.我通过设置属性的值来做到这一点.在某些情况下,此默认值会覆盖protobuf数据中的值.

这是一个演示:

public class Program
{
    static void Main(string[] args)
    {
        var target = new MyClass
        {
            MyBoolean = false
        };

        using (var stream = new MemoryStream())
        {
            Serializer.Serialize(stream, target);
            stream.Position = 0;
            var actual = Serializer.Deserialize<MyClass>(stream);
            //actual.MyBoolean will be true
        }
    }
}

[ProtoContract(Name = "MyClass")]
public class MyClass
{
    #region Properties

    [ProtoMember(3, IsRequired = false, Name = "myBoolean", DataFormat = DataFormat.Default)]
    public Boolean MyBoolean { get; set; } = true;

    #endregion
}
Run Code Online (Sandbox Code Playgroud)

反序列化数据后,MyBoolean的值为true.

我该如何解决这个问题?

c# protobuf-net

3
推荐指数
1
解决办法
1404
查看次数

如何确保从NetworkStream读取所有数据

当DataAvailable为false时,确保从NetworkStream读取所有数据吗?

或者数据的发送者是否必须首先发送数据的长度.我必须阅读,直到我读取发件人指定的字节数?

Sampel:

private Byte[] ReadStream(NetworkStream ns)
{
    var bl = new List<Byte>();
    var receivedBytes = new Byte[128];
    while (ns.DataAvailable)
    {
            var bytesRead = ns.Read(receivedBytes, 0, receivedBytes.Length);
            if (bytesRead == receivedBytes.Length)
                bl.AddRange(receivedBytes);
            else
                bl.AddRange(receivedBytes.Take(bytesRead));
    }
    return bl.ToArray();
}
Run Code Online (Sandbox Code Playgroud)

c# tcp networkstream

2
推荐指数
1
解决办法
3282
查看次数

将byte []转换为String并返回c#

我正在尝试使用Encoding.Unicode将byte []转换为字符串并返回.有时Encoding.Unicode能够将byte []转换为字符串,有时输出是!=输入.我究竟做错了什么?

谢谢你的帮助.

public static void Main(string[] args)
{
    Random rnd = new Random();
    while(true)
    {
        Int32 random = rnd.Next(10, 20);
        Byte[] inBytes = new Byte[random];
        for(int i = 0; i < random; i++)
            inBytes[i] = (Byte)rnd.Next(0, 9);

        String inBytesString = Encoding.Unicode.GetString(inBytes, 0, inBytes.Length);
        Byte[] outBytes = Encoding.Unicode.GetBytes(inBytesString);

        if(inBytes.Length != outBytes.Length)
            throw new Exception("?");
        else
        {
            for(int i = 0; i < inBytes.Length; i++)
            {
                if(inBytes[i] != outBytes[i])
                    throw new Exception("?");
            }
        }
        Console.WriteLine("OK");
    }
}
Run Code Online (Sandbox Code Playgroud)

c# string byte

1
推荐指数
1
解决办法
2003
查看次数

使用 DataContractJsonSerializer 读取 JSON

我正在开发一个访问 TMDb 的应用程序。我使用 DataContractJsonSerializer 类来读取 TMDb API 返回的数据。

有一种 API 方法返回一个 JSON,它可能如下所示:

{"id":550,"favorite":false,"rated":false,"watchlist":false}
Run Code Online (Sandbox Code Playgroud)

或喜欢:

{"id":49521,"favorite":false,"rated":{"value":5.5},"watchlist":false}
Run Code Online (Sandbox Code Playgroud)

如您所见,第一个 JSON 中的 rating 字段是一个布尔值,而在第二个中则不是。
有没有处理这个 JSON 的好方法?

谢谢你的帮助

编辑: 读取 JSON:

[DataContract]
public class JsonModelBase<T> where T : class, new()
{
    public T DesterilizeJson(String jsonContent)
    {
        var bytes = Encoding.UTF8.GetBytes(jsonContent);
        using (var stream = new MemoryStream(bytes))
            return DesterilizeJson(stream);
    }
    public T DesterilizeJson(Stream jsonStream)
    {
        var serializer = new DataContractJsonSerializer(typeof(T));
        return (T)serializer.ReadObject(jsonStream);
    }
}

[DataContract]
public class TmdbMovieAccountStates : JsonModelBase<TmdbMovieAccountStates>
{
    [DataMember(Name = "id")]
    public …
Run Code Online (Sandbox Code Playgroud)

.net c# json

1
推荐指数
1
解决办法
1万
查看次数

Visual Studio 2015缺少新关键字 - 没有编译器错误

当我犯了一个愚蠢的错误时,我正在处理我们的一个新应用程序......我忘了写一个对象初始化程序中的"new ClassName".奇怪的是VS刚刚编译了代码......没有任何错误或警告.(我用过VS 2015,.Net 4.5.2)

我的代码:

var target = new MyClass
{
    MyValues = new List<MyOtherClass>
    {
        new MyOtherClass
        {
            Start = new DateTimeValue
            {
                DateTime = new DateTime(2015, 08, 23)
            },
            End = {
                DateTime = new DateTime(2015, 08, 23)
            }
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

(开始和结束都是DateTimeValue类型)

当我启动应用程序时,此代码抛出了NullReferenceException.(添加"new DateTimeValue"修复了问题).为什么编译器没有错误?

c# visual-studio object-initializer c#-6.0

0
推荐指数
1
解决办法
193
查看次数

从记录中获取属性

我正在寻找一种方法来获取记录构造函数“字段”上定义的属性。

// See https://aka.ms/new-console-template for more information

using System.ComponentModel.DataAnnotations;

var property = typeof(TestRecord)
               .GetProperties()
               .First( x => x.Name == nameof(TestRecord.FirstName) );

var attr0 = property.Attributes; // NONE
var attr1 = property.GetCustomAttributes( typeof(DisplayAttribute), true ); // empty

var property1 = typeof(TestRecord)
                .GetProperties()
                .First( x => x.Name == nameof(TestRecord.LastName) );

var attr2 = property1.Attributes; // NONE
var attr3 = property1.GetCustomAttributes( typeof(DisplayAttribute), true ); // Works

public sealed record TestRecord( [Display] String FirstName, [property: Display] String LastName );
Run Code Online (Sandbox Code Playgroud)

我能够获取针对LastName该属性的属性(使用property:)。 …

c# reflection record

0
推荐指数
1
解决办法
1199
查看次数