小编Cha*_*ell的帖子

如何解码位以获取原始值?

假设我有以下枚举

[Flags]
enum Options
{
    Option1 = 1 << 0, 
    Option2 = 1 << 1, 
    Option3 = 1 << 2
}
Run Code Online (Sandbox Code Playgroud)

我要设置如下变量

var options = 0;
options |= Options.Option1; 
options |= Options.Option3; 

// now options should equal Option1 + Option3
// I then store that single value in the database
myDatabase.Options.Submit(options);
Run Code Online (Sandbox Code Playgroud)

然后,我如何解析"选项"以获取原始值?

public List<Options> ParseOptions(Options options)
{
    // Not sure how to parse the options.
}
Run Code Online (Sandbox Code Playgroud)

c# parsing bit-manipulation

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

解析系统日志优先级值的算法(PRIVAL)

我想PRIVAL从 syslog 条目中解析信息,但我无法理解所需的算法。

RFC5424说:

Priority 值的计算方法是首先将 Facility 编号乘以 8,然后加上 Severity 的数值。

有了这个,这就是我所理解的。

(X * 8) + y = [known number]
Run Code Online (Sandbox Code Playgroud)

所以

If (X * 8) + Y = 134    
// I happen to know that X = 16 and Y = 6
Run Code Online (Sandbox Code Playgroud)

或者

If (X * 8) + Y = 78
// What are the values of X and Y?
Run Code Online (Sandbox Code Playgroud)

那么解析这些信息的合适算法是什么?

.net c# bit-manipulation syslog

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

快速获取两个List <>对象之间的区别

我如何itemsToRemove只包含"bar one",并且itemsToAdd只包含"bar five"?

我正在尝试使用"除外",但显然我使用它不正确.

var oldList = new List<Foo>();
oldList.Add(new Foo(){ Bar = "bar one"});
oldList.Add(new Foo(){ Bar = "bar two"});
oldList.Add(new Foo(){ Bar = "bar three"});
oldList.Add(new Foo(){ Bar = "bar four"});



var newList = new List<Foo>();
newList.Add(new Foo(){ Bar = "bar two"});
newList.Add(new Foo(){ Bar = "bar three"});
newList.Add(new Foo(){ Bar = "bar four"});
newList.Add(new Foo(){ Bar = "bar five"});


var itemsToRemove = oldList.Except(newList);    // should only contain "bar one"
var itemsToAdd = newList.Except(oldList); …
Run Code Online (Sandbox Code Playgroud)

c# list

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

为什么在生成Random()数字时会得到奇怪的结果?

我的计划需要:

一个.生成一个由0到9的20个随机整数的数组.搜索数字7的第一个匹配项(如果有),并在数组中报告其位置.

湾 重复计算部分a 1000次,并对数组中的每个位置报告数组中第一次出现7的位置在该位置的次数

但是每当我运行程序时,我都会得到奇怪的结果(每次都不同),例如:

  1. 在任何位置都没有发现任何七人制
  2. 在一个位置找到1000个七人制,在其他地方找不到七人制
  3. 在2个位置发现了数百个七人组,其他地方都找不到.

有谁知道我的程序有什么问题?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Week_6_Project_2
    {
    class Program
    {

    static int intArrayLength = 20;
    static int[] resultsArray = new int[intArrayLength];

    public static Array generateRandomArray() {
        int[] randomNumberArray = new int[intArrayLength];
        Random random = new Random();
        int popcounter = 0;
        while (popcounter < intArrayLength) {
            randomNumberArray[popcounter] = random.Next(0, 10);
            popcounter += 1;
        }
        return randomNumberArray;
    }

    public static void searchForSevens()
    {
        int counter = …
Run Code Online (Sandbox Code Playgroud)

c# arrays random

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

使用Func和依赖注入的Autofac动态调用

假设我有一个课程如下

public class Foo
{
    public Foo(string someTitle, IFooService fooService)
    { 
        // do stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用DI和autofac这样实例化它

public class Bar
{
    public Bar(Func < string, IFooService, Foo > foo, IFooService fooService)
    {
        var foo = foo("some string", fooService);
    }
}
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有任何方法Bar可以不知道任何事情IFooService?我不想为了满足func而将IFooService注入Bar.

基本上是这样的

// pseudo code - don't use
public class Bar
{
    public Bar(Func < string, Foo > foo)
    {
        var foo = foo("some string");
    }
}
Run Code Online (Sandbox Code Playgroud)

我在我的应用程序中真正想做的是删除所有服务位置实例,并完全依赖依赖注入.

c# dependency-injection autofac service-locator

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

如何使用 FluentValidation 验证集合中的不同类型?

我有一个需要验证的集合的类。集合上的泛型采用一个接口,可以将不同的类型添加到集合中。

创建支持多态性的 FluentValidation 验证器的最简洁路径是什么?

public interface IWizardStep {}

public class WizardOne : IWizardStep
{
    public string Model { get; set; }
}

public class WizardTwo : IWizardStep
{
    public string FirstName { get; set; }
}

public class Wizard
{
    public Wizard()
    {
        var w1 = new WizardOne();
        var w2 = new WizardTwo();

        Steps = new List<IWizardStep>
                    {
                        w1,
                        w2
                    };
    }

    public IList<IWizardStep> Steps { get; set; }
}

public class WizardValidator : AbstractValidator<Wizard>
{
    public WizardValidator()
    {
        RuleFor(x …
Run Code Online (Sandbox Code Playgroud)

fluentvalidation

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

你会如何简化这种映射?数组到对象

你认为什么是最img.Group.Contents简洁的方法来循环并写出galleryImage的值.??? 对象?

galleryImage.TinyImage = new myModel.Image._Img();

galleryImage.TinyImage.Url = img.Group.Contents[0].Url;
galleryImage.TinyImage.FileSize = img.Group.Contents[0].FileSize;
galleryImage.TinyImage.Type = img.Group.Contents[0].Type;
galleryImage.TinyImage.Medium = img.Group.Contents[0].Medium;
galleryImage.TinyImage.Width = img.Group.Contents[0].Width;
galleryImage.TinyImage.Height = img.Group.Contents[0].Height;
galleryImage.TinyImage.Hash = img.Group.Contents[0].Hash;

galleryImage.Thumbnail.Url = img.Group.Contents[1].Url;
galleryImage.Thumbnail.FileSize = img.Group.Contents[1].FileSize;
galleryImage.Thumbnail.Type = img.Group.Contents[1].Type;
galleryImage.Thumbnail.Medium = img.Group.Contents[1].Medium;
galleryImage.Thumbnail.Width = img.Group.Contents[1].Width;
galleryImage.Thumbnail.Height = img.Group.Contents[1].Height;
galleryImage.Thumbnail.Hash = img.Group.Contents[1].Hash;

galleryImage.SmallImage.Url = img.Group.Contents[2].Url;
galleryImage.SmallImage.FileSize = img.Group.Contents[2].FileSize;
galleryImage.SmallImage.Type = img.Group.Contents[2].Type;
galleryImage.SmallImage.Medium = img.Group.Contents[2].Medium;
galleryImage.SmallImage.Width = img.Group.Contents[2].Width;
galleryImage.SmallImage.Height = img.Group.Contents[2].Height;
galleryImage.SmallImage.Hash = img.Group.Contents[2].Hash;

galleryImage.MediumImage.Url = img.Group.Contents[3].Url;
galleryImage.MediumImage.FileSize = img.Group.Contents[3].FileSize;
galleryImage.MediumImage.Type = img.Group.Contents[3].Type; …
Run Code Online (Sandbox Code Playgroud)

c# arrays loops

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

如何在C#web服务web.config中使用密码,其中包含"&"

我无法在我的连接字符串中使用密码,密码中包含"&",我的服务会抛出500内部服务器错误.

当我从密码中删除"&"时,服务运行但显然我不会连接到数据库.

例如

< add name="conn" connectionString="Data Source=DFF1234;Initial Catalog=PONJ1;User Id=PPID1;Password=POu&3mjl@sdRTYu" providerName="System.Data.SqlClient" >
Run Code Online (Sandbox Code Playgroud)

c#

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

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