小编Nad*_*ern的帖子

C#中的静态构造函数

我试图使用如下的静态构造函数:

public static DataManager()
{
    LastInfoID = 1;
}
Run Code Online (Sandbox Code Playgroud)

并收到此错误:

静态构造函数不允许访问修饰符

我想知道我的问题是什么.

c# static constructor

42
推荐指数
4
解决办法
2万
查看次数

在c#中使用互斥锁

我在c#的线程中有点新,在一般情况下,在我的程序中我mutex只允许1个线程进入一个关键部分并且由于未知的原因做了一些cw打印我可以看到超过1个线程进入我的内部关键部分,这是我的代码:

Mutex m = new Mutex();
m.WaitOne();
<C.S> // critical section here
m.ReleaseMutex();
Run Code Online (Sandbox Code Playgroud)

我非常想知道我是否在这里犯了一个错误,在此先感谢您的帮助.

编辑:

我的代码包括类,所以它基本上看起来像这样:

public class test
{
    private mutex m;
    public test()
    {
         m = new mutex();
    }
    public func()
    {
         m.WaitOne();
         <C.S> // critical section here
         m.ReleaseMutex();
     }


    } 
Run Code Online (Sandbox Code Playgroud)

c# multithreading mutex

32
推荐指数
3
解决办法
6万
查看次数

使用触发器在鼠标悬停时设置文本块前景

我试图设置块,所以每次鼠标光标越过它时它的前景色会改变,这是我的代码:

<TextBlock Foreground="blue" Margin="18,234,5,-2" Grid.RowSpan="3">
    <Underline>Remove Message</Underline>
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <Trigger Property ="IsMouseOver" Value="True">
                    <Setter Property= "Foreground" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

例如,当我尝试将setter属性更改为任何其他属性时FontSize="30",事件确实发生.

wpf xaml

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

数据类:如何使用 asdict() 忽略默认值?

我想在调用 asdict() 后忽略默认值

@dataclass
class A:
    a: str
    b: bool = True
Run Code Online (Sandbox Code Playgroud)

所以如果我打电话

a = A("1")
result = asdict(a, ignore_default=True) 
assert {"a": "1"} == result  # the "b": True should be deleted
Run Code Online (Sandbox Code Playgroud)

python python-dataclasses

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

在jar文件中包含一个文本文件并读取它

可能重复:
Java资源作为文件

我是Java的新手,我试图在Jar文件中获取一个文本文件.

在我执行我的jar时,我必须将我的文本文件放在与jar文件相同的文件夹中.如果文本文件不存在,我会得到一个NullPointerException,我想避免.

我想要做的是在jar中获取txt文件,所以我不会遇到这个问题.我尝试了一些指南,但它们似乎没有用.我目前的读取功能如下:

public static HashSet<String> readDictionary()
{
    HashSet<String> toRet = new HashSet<>();
     try
     {
            // Open the file that is the first 
            // command line parameter
            FileInputStream fstream = new FileInputStream("Dictionary.txt");
        try (DataInputStream in = new DataInputStream(fstream)) {
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            //Read File Line By Line
            while ((strLine = br.readLine()) != null)   {
            // Read Lines
                toRet.add(strLine);
            }
        }
            return toRet;
     }
     catch (Exception e)
     {//Catch exception if any
            System.err.println("Error: …
Run Code Online (Sandbox Code Playgroud)

java jar nullpointerexception embedded-resource

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

在C#中执行包含GO语句的SQL批处理

我正在尝试构建一个程序,它在批处理中执行sql语句并进行错误处理(因此我没有使用SMO).

问题是GO不是SQL的一部分,当使用.NET执行语句时,它最终会出现错误(SMO会处理它,但不会指示执行是否失败).

string statements = File.ReadAllText("c:\\test.sql");
string[] splitted = statements.split("GO");
Run Code Online (Sandbox Code Playgroud)

使用上面的行不能解决我的问题,因为GO关键字也可以进入注释(我不想从语句中删除注释)并且注释可以进入/**/或在两个破折号之后-
例如,我不希望解析以下代码:

/*
GO
*/
Run Code Online (Sandbox Code Playgroud)

(ofc我谷歌搜索它,但那里没有解决方案)

.net c# sql sql-server

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

在C#中克隆一个对象

我想使用ICloneable界面克隆一个对象,由于某种原因我不能克隆我的程序.这是我的代码:

public class GeoInfo : ICloneable
{
    private long InfoID;
    private string InfoName;
    private Location InfoLocation;
    private string Description;
    private InfoTypes InfoType;
    public GeoInfo(long InfoID)
    {

        this.InfoID = InfoID;
    }
    public GeoInfo(long InfoID, Location InfoLocation):this(InfoID)
    {
        this.InfoLocation = InfoLocation;
    }
    public GeoInfo(long InfoID, string InfoName, Location InfoLocation, string Description, InfoTypes InfoType):this(InfoID,InfoLocation)
    {
        this.InfoName = InfoName;
        this.Description = Description;
        this.InfoType = InfoType;
    }
    public object ICloneable.Clone()
    {
        GeoInfo toReturn = new GeoInfo(InfoID, InfoName, InfoLocation, Description, InfoType);
        return (object)toReturn;
    } …
Run Code Online (Sandbox Code Playgroud)

c# icloneable

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

使用触发器设置按钮IsEnabled在鼠标悬停时为true

我试图设置一个按钮,所以只有当鼠标移过它时,按钮才会将其视觉属性更改为启用按钮.

这是我的代码:

    <Button Foreground="Black" Content="OK" Margin="186,100,170,172" >
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="IsEnabled" Value="False" />
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="IsEnabled" Value="True" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助.

wpf xaml styles isenabled

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

用文本填充画布形状

我想弄清楚如何将文本添加到画布形状,例如这里是我的代码:

var text ="5"; // text to display over the circle
context.fillStyle = "red";
context.beginPath();
context.arc(50,70, 10, 0, Math.PI * 2);
context.closePath();
context.fill(); 
Run Code Online (Sandbox Code Playgroud)

如果有人愿意帮助我提前将文字添加到形状中,我会非常感激.

编辑 我发现我需要在画布上再次写,所以这是我到目前为止所得到的...但是文本并没有与圆圈的中心对齐:

  context.fillStyle = "red";
  context.beginPath();
  var radius = 10; // for example
  context.arc(200, 200, radius, 0, Math.PI * 2);
  context.closePath();
  context.fill();
  context.fillStyle = "black"; // font color to write the text with
  var font = "bold " + radius +"px serif";
  context.font = font;
  context.textBaseline = "top";
  context.fillText(text, 200-radius/4 ,200-radius/2);
Run Code Online (Sandbox Code Playgroud)

html5 drawing canvas

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

Angularjs ui-grid排序表根据给定的列名称

我正在使用ui-grid版本v3.0.0-rc.21-1d9f81f - 2015-05-01并尝试根据给定的列名对给定的网格进行排序.我不能在外部(从javascript代码)对它进行排序,只能通过定义它并单击网格的标题.

在这个版本中是否可以通过javascript代码根据网格的列名对网格数据进行排序?

angularjs angular-ui-grid

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