小编Tho*_*ser的帖子

在静态字段初始化程序中使用非静态字段的nameof

我有一个带有List<T>静态字段和一些非静态字段的类。现在,我想使用一些对象来初始化此列表,如下所示:

class C
{
    private Bar bar;

    private static List<Foo> list = new List<Foo>()
    {
        new Foo(nameof(bar)),                    // OK
        new Foo(nameof(bar) + nameof(bar.Baz))   // Error CS0236
    };
}
Run Code Online (Sandbox Code Playgroud)

虽然创建第一个Foo对象可以正常工作,但出现错误

字段初始值设定项不能引用非静态字段,方法或属性C.bar

在第二。

我知道静态成员无法访问实例数据(请参阅此问题)。但是据我了解,它nameof是在编译时进行评估的,并且仅访问元数据而不访问实例数据。那么,为什么这项工作不起作用,还有没有解决的办法呢?

c# nameof

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

c#typeof应用于泛型参数超类型

我有一个方法,它没有任何T参数.

遗憾的是,如下所示,C#typeof运算符不适用于泛型.

public static string ToString(object obj)
{
    if (obj.GetType() == typeof(List<object>))
    {
        return "Bug: Never called";
    }
    if (obj.GetType() == typeof(List<int>))
    {
        return "Method only works for int lists";
    }
    return "";
}
Run Code Online (Sandbox Code Playgroud)

预期输出:ToString(整数列表)将返回"Bug:Never Called".

实际输出:"方法仅适用于int列表"

这在Java中运行得非常好.你会说c#中的泛型不支持这个.如何重构我的代码以使"ToString(object)"方法适用于对象列表?

要求:您不能修改方法的签名,只能修改它的实现.

不幸的是你不能让"int"类实现一个接口.因为int类是由Microsoft编写的.因此我无法在int类型上添加访问者模式.

c# generics wildcard typeof

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

Ada:循环导致后面的代码无法访问

我是艾达的新手。我正在尝试使以下代码工作:

begin
Ada.Text_IO.Put_Line("Student ID Score");
Ada.Text_IO.Put_Line("===================");
readAnswers(pAnswers, 1);

loop
declare
   counter : Integer := 0;
   studentInput : String := Get_Line(Input);
   studentScore : Integer;
begin
   numOfTests := numOfTests + 1;
   current_student.ID := GetID(studentInput);
   Ada.Text_IO.Put(Ada.Strings.Unbounded.To_String(current_student.ID));
   readAnswers(studentInput (6 .. studentInput'Last) ,0);
   studentScore := scoreTest(current_student.student_answer, current_answer_key, number_of_questions);
      Ada.Integer_Text_IO.Put(studentScore);
      New_Line(1);
end;
end loop;

Ada.Text_IO.Put_Line("===================");
Ada.Text_IO.Put("Tests Graded = ");
Ada.Integer_Text_IO.Put(numOfTests);
end;
Run Code Online (Sandbox Code Playgroud)

不幸的是,GNAT 告诉我循环之后的所有代码都无法访问。我怎样才能让这个程序执行循环和它之后的代码?

ada

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

C#为结构错误的属性赋值

我有以下代码(简化)、一个结构和一个类。

public struct pBook
{
    private int testID;

    public string request;
    public string response;
    public Int32 status;
    public int test_id
    {
        get
        {
            return testID;
        }
        set
        {
            testID = value;
        }
    }
};

public class TestClass
{
    public static void Main(string[] args)
    {
        pBook Book1;
        pBook Book2;

        Book1.request = "a";
        Book2.response = "b";
        Book2.status = 201;
        Book2.test_id = 0;  //this doesn't work, why?
    }
}
Run Code Online (Sandbox Code Playgroud)

在声明中

Book2.test_id = 0;
Run Code Online (Sandbox Code Playgroud)

我收到错误

使用未分配的局部变量“Book2”

任何想法如何纠正?

c# structure

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

Ada 重叠标签字段

这是我的类型:

package MyPackage is
    Type T_MyType is record
        Field1 : Uint16;
        Field2 : Uint32;
        Field3 : Uint8;
        Field4 : Uint8;
    end record;
private
    for T_MyType'Alignment use 4;
    for T_MyType'Size use 64;
    for T_MyType use record
        Field1 at 16#00# range 0 .. 15;
        Field2 at 16#02# range 0 .. 31;
        Field3 at 16#06# range 0 .. 7;
        Field4 at 16#06# range 8 .. 15:
    end record
end package
Run Code Online (Sandbox Code Playgroud)

我没有错误,但是如果我将类型更改Type T_MyType is tagged record为第一行,则会出现错误:

组件与“T_MyType”的标签字段重叠

标记记录是否有隐藏字段?如何使用标记记录保留我的地址?

record ada

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

CFSCRIPT - For Loop - 如何递增50或100?

我正在使用ColdFusion 9.我对非CFSCRIPT答案不感兴趣.

我试图以5的增量从0循环到100.

这是我的正常循环:

for (i = 0; i < 100; i + 5) {
    writeOutput(i);
}
Run Code Online (Sandbox Code Playgroud)

为什么这不适合我?

++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++

for (i = 0; i < 100; i + 5) {
    writeOutput(i);
}
Run Code Online (Sandbox Code Playgroud)

coldfusion loops

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

如何使用 ggplot2 在条形图轴上创建空因子水平的占位符?

我有一些调查数据,我想从中简单地绘制出每个问题的每个响应的频率。

我的代码

library(ggplot2)

Df1 # data frame with 3 variables: Provider, Question, Score

Df1$Score <- factor(Df1$Score, levels = c(0,1,2,3))

# Plot
q <- qplot(Score, data = Df1, facets = .~Provider, geom="bar", fill=Provider)

q + labs(title = wrapper(Question, width = 70), x = "Score", y = "Frequency")   # Add title and axes labels
Run Code Online (Sandbox Code Playgroud)

结果是这样的: 在此输入图像描述

这很棒,但由于我将生成其中一些图表,如果 x 轴上也有“1”的空格,那么在视觉上更容易解释。

我花了很多时间尝试各种组合scale_x_discretelabels但我breaks仍然reorder无法生成一个难以捉摸的占位符。

有没有一种简单的方法可以强制 ggplot2 绘制空因子?

axis r histogram ggplot2

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

使用 Python 实现带有 Sockets(不带 http 库)的 HTTP 客户端

出于教育目的,但没有任何重要性,我想实现一个脚本,可以发出简单的 HTTP 请求并在控制台上显示答案的内容(以纯文本形式)。我用这段代码实现了它:

import socket
import sys

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server_address = ('localhost', 8080)
print >>sys.stderr, 'connecting to %s port %s' % server_address
sock.connect(server_address)

message = 'GET /php.php HTTP/1.1\r\n'
message += 'Host: localhost:8080\r\n\r\n'
print >>sys.stderr, 'sending "%s"' % message
sock.sendall(message)

data = sock.recv(10000000)
print >>sys.stderr, 'received "%s"' % data

sock.close()
Run Code Online (Sandbox Code Playgroud)

我只是构建 HTTP 请求,将其发送到服务器,然后等待答复。

现在问题来了:我不知道如何阅读整个答案,我知道有一个标题是“content-lengt”(让我们假设它永远存在)。我怎样才能不读答案的全部内容呢sock.recv (1000000000000000000)

python http request

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

带有可为空字段的 EqualityComparer 的奇怪行为

假设有这个类:

public class Foo
{
    public int Id { get; set; }
    public int? NullableId { get; set; }

    public Foo(int id, int? nullableId)
    {
        Id = id;
        NullableId = nullableId;
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要按照以下规则比较这些对象:

  1. 如果两个对象都有 NullableId 的值,那么我们比较 Id 和 NullableId
  2. 如果某些对象/它们都没有 NullableId,则忽略它并仅比较 Id。

为了实现它,我像这样覆盖了 Equals 和 GetHashCode:

public override bool Equals(object obj)
{
    var otherFoo = (Foo)obj;

    var equalityCondition = Id == otherFoo.Id;

    if (NullableId.HasValue && otherFoo.NullableId.HasValue)
        equalityCondition &= (NullableId== otherFoo.NullableId);

    return equalityCondition;
}

public override int GetHashCode()
{
    var …
Run Code Online (Sandbox Code Playgroud)

.net c# nullable iequalitycomparer

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

作为对象参数传递时,无效的强制转换异常

假设我有两个类,其中一个可以转换为另一个:

public class Foo { }

public class Bar
{
    public static implicit operator Foo(Bar bar) => new Foo();
}
Run Code Online (Sandbox Code Playgroud)

现在我有一个方法,它期望一个对象作为它所投射的参数Foo.

public void DoSomething(object o)
{
    var foo = (Foo)o;
   /* Do something with foo here */
}
Run Code Online (Sandbox Code Playgroud)

现在我这样调用这个方法:

var bar = new Bar();
var foo = (Foo)bar;

DoSomething(foo);
DoSomething(bar);
Run Code Online (Sandbox Code Playgroud)

当呼叫DoSomething(foo)按预期工作时,呼叫DoSomething(bar)会抛出一个InvalidCastException.为什么运行时不能在第二个方法调用中使用用户定义的类型转换运算符?

c# casting

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