小编com*_*cme的帖子

将PDF转换为PNG

我正在尝试将PDF转换为PNG图像(至少是一个封面).我用pdftk成功地提取了PDF的第一页.我正在使用imagemagick进行转换:

convert cover.pdf cover.png
Run Code Online (Sandbox Code Playgroud)

这很有效,但不幸的是,cover.png通过错误渲染(PDF中的某些alpha对象无法正确呈现).我知道ImageMagick使用GhostScript进行转换,如果我直接使用gs我可以得到所需的结果,但我宁愿使用转换库,因为它有其他我想要利用的工具.

GhostScript中的此命令可完成所需的图像:

gs -sDEVICE=pngalpha -sOutputFile=cover.png -r144 cover.pdf
Run Code Online (Sandbox Code Playgroud)

我想知道有没有办法通过转换为GhostScript传递参数,还是我坚持直接调用GhostScript?

pdf png image imagemagick ghostscript

76
推荐指数
5
解决办法
9万
查看次数

如何让重载的构造函数调用默认构造函数以及基本构造函数的重载?

也许我说过的问题不是正确的问题,因为我已经知道简短的回答是"你不能".

情况

我有一个带有重载构造函数的基类,它带有两个参数.

class Building
{
    public BuildingType BuildingType { get; protected set; }
    public string Address { get; set; }
    public decimal Price { get; set; }

    public Building()
    {
        BuildingType = BuildingType.General;
        Address = "Unknown";
    }

    public Building(string address, decimal price)
        : this()
    {
        Address = address;
        Price = price;
    }
}
Run Code Online (Sandbox Code Playgroud)

该课程正在使用枚举

enum BuildingType { None, General, Office, Apartment }
Run Code Online (Sandbox Code Playgroud)

现在我想创建一个子类Office,它也有一个重载的构造函数.这个子类添加了另一个属性(公司).在此类中,BuildingType属性当然应设置为Office.这是代码.

class Office : Building
{
    public string Company { get; set; }

    public Office()
    {
        BuildingType …
Run Code Online (Sandbox Code Playgroud)

c# constructor

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

为什么我们不能在if语句中定义变量?

也许这个问题之前已经得到了回答,但是这个词if经常出现,很难找到它.

这个例子没有意义(表达式总是正确的),但它说明了我的问题.

为什么此代码有效:

StringBuilder sb;
if ((sb = new StringBuilder("test")) != null) {
    Console.WriteLine(sb);
}
Run Code Online (Sandbox Code Playgroud)

但是这段代码不是:

if ((StringBuilder sb = new StringBuilder("test")) != null) {
    Console.WriteLine(sb);
}
Run Code Online (Sandbox Code Playgroud)

我发现了一个关于while声明的类似问题.那里接受的答案说,在一个while声明中,它意味着变量将在每个循环中定义.但是对于我的if陈述例子,情况并非如此.

那么我们不允许这样做的原因是什么?

c# scope language-design

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

具有公共getter的抽象属性,可以在具体类中定义私有setter吗?

我正在尝试创建一个使用getter定义属性的抽象类.我想将它留给派生类来决定是否要为属性实现setter.这可能吗?

到目前为止我所拥有的:

public abstract class AbstractClass {
    public abstract string Value { get; }
    public void DoSomething() {
        Console.WriteLine(Value);
    }
}

public class ConcreteClass1 : AbstractClass {
    public override string Value { get; set; }
}

public class ConcreteClass2 : AbstractClass {
    private string _value;
    public override string Value {
        get { return _value; }
    }
    public string Value {
        set { _value = value; }
    }
}

public class ConcreteClass3 : AbstractClass {
    private string _value;
    public override string …
Run Code Online (Sandbox Code Playgroud)

c# getter setter abstract-class properties

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

获取当前配置文件的文件名

我认为这很简单,但我找不到答案.

我正在使用远程处理,我想在app.config中存储RemotingConfiguration.当我打电话时,RemotingConfiguration.Configure我必须提供远程信息所在的文件名.所以......我需要知道当前配置文件的名称.

目前我正在使用这个:

System.Reflection.Assembly.GetExecutingAssembly().Location + ".config"
Run Code Online (Sandbox Code Playgroud)

但这仅适用于Windows应用程序,不适用于Web应用程序.

是不是有任何类可以为我提供当前配置文件的名称?

c# configuration remoting app-config

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

使用语句和添加引用有什么区别?

在Visual Studio中,何时需要添加对dll的引用?我总是尝试在我的项目中使用最少的引用,我尝试只包括那些非常必要的引用.

如果我using在源代码中有声明,我认为我只需要一个引用.但这并不总是足够的.

例如,我有一个非常简单的程序,即usingSystem和Microsoft.Practices.EnterpriseLibrary.Data:

using System;
using Microsoft.Practices.EnterpriseLibrary.Data;

public class SimpleConnection {
    private static void Main() {
        var database = DatabaseFactory.CreateDatabase();
        var command =
            database.GetSqlStringCommand(
                "select table_name from information_schema.tables");
        using (var reader = database.ExecuteReader(command)) {
            while (reader.Read()) {
                Console.WriteLine(reader.GetString(0));
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我只需要引用System和Microsoft.Practices.EnterpriseLibrary.Data.但事实并非如此.如果我不引用System.Data,代码将无法编译.

"System.Data.Common.DbCommand"类型在未引用的程序集中定义.您必须添加对程序集'System.Data,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'的引用.

当我必须添加对某些我不是的引用时,我怎么能事先知道using

c# visual-studio

17
推荐指数
3
解决办法
9112
查看次数

在可空类型上使用coalescing null运算符更改隐式类型

我希望接下来的三行代码是相同的:

public static void TestVarCoalescing(DateTime? nullableDateTime)
{
  var dateTimeNullable1 = nullableDateTime.HasValue ? nullableDateTime : DateTime.Now;
  var dateTimeNullable2 = nullableDateTime != null ? nullableDateTime : DateTime.Now;
  var dateTimeWhatType = nullableDateTime ?? DateTime.Now;
}
Run Code Online (Sandbox Code Playgroud)

在所有情况下,我都分配nullableDateTime给新变量.我希望所有变量的类型都成为,DateTime?因为那是类型nullableDateTime.但令我惊讶的是,这种类型dateTimeWhatType只是变成了DateTime,所以不可空.

更糟糕的是,ReSharper建议用空合并表达式替换第二个语句,将其转换为表达式3.因此,如果我让ReSharper执行它的操作,变量的类型将从更改DateTime?DateTime.

事实上,让我们说在方法的其余部分,我会使用

if (someCondition) dateTimeNullable2 = null;
Run Code Online (Sandbox Code Playgroud)

那会编译得很好,直到我让ReSharper用空合并版本替换第二个表达式.

AFAIK,取代

somevar != null ? somevar : somedefault;
Run Code Online (Sandbox Code Playgroud)

somevar ?? somedefault;
Run Code Online (Sandbox Code Playgroud)

应该确实产生相同的结果.但是对于可空类型的隐式类型,编译器似乎威胁??就像它意味着一样.

somevar != null ? somevar.Value : somedefault;
Run Code Online (Sandbox Code Playgroud)

所以我想我的问题是为什么隐藏类型在我使用时会被更改??,而且在文档中我可以找到关于此的信息. …

c# resharper implicit null-coalescing-operator

14
推荐指数
2
解决办法
5875
查看次数

WinForms ComboBox SelectedIndexChanged在键入少量字符后跟Alt + Down时不触发

简而言之

当我在ComboBox中键入一个字符时,按Alt +向下键,然后按Enter键或Tab键,即使SelectedIndex值确实更改,也不会触发SelectedIndexChanged事件!为什么事件没有发生?

更新 如果键入字符,则按Alt +向下键,然后键入Esc,则会出现相同的错误.您可能希望Esc取消更改.但是,SelectedIndex 更改,并且不会触发SelectedIndexChanged事件.

如果只键入Alt + Down,使用箭头键浏览条目,然后键入Esc,会发生什么?是否应将所选索引设置回其原始值?


不是那么短暂

我有一个带有ComboBox的WinForm应用程序.ComboBox的SelectedIndexChanged事件连接到一个事件处理程序,该事件处理程序显示Label控件中的SelectedItem.ComboBox的Items集合有三个值:"One","Two"和"Three".

  • 当我用鼠标选择项目时,事件将触发.
  • 滚动鼠标时,事件会触发.
  • 当我使用Alt + Down扩展组合框并使用向上和向下遍历项目时,事件将触发.
  • 但是......当我输入值的第一个字符时,然后按Alt + Down,然后按Enter或Tab,值将被选中并显示在组合框中,但事件不会触发.

我还添加了一个显示SelectedIndex的按钮.它显示SelectedIndex 更改.因此,即使SelectedIndex确实发生了变化,SelectedIndexChanged事件也不会触发!

如果我只输入一个有效的值,就像 One 事件不会触发一样,但在这种情况下点击按钮会显示SelectedIndex确实没有改变.所以在这种情况下行为是正常的.


要重现,请创建一个表单并添加一个ComboBox,一个Label和一个Button.将以下代码放在Form1.cs中:

using System;
using System.Windows.Forms;

namespace ComboBoxSelectedIndexChanged
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            comboBox1.Items.AddRange(new object[] {
                "One",
                "Two",
                "Three"
            });
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label1.Text = "Selected index: " + comboBox1.SelectedIndex;
        }

        private void button1_Click(object sender, EventArgs …
Run Code Online (Sandbox Code Playgroud)

c# events combobox winforms

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

如何确定两个泛型类型值是否相等?

更新* 我很抱歉......我的示例代码包含一个错误,导致了许多我不理解的答案.代替

Console.WriteLine("3. this.Equals   " + (go1.Equals(go2)));
Run Code Online (Sandbox Code Playgroud)

我打算写

Console.WriteLine("3. this.Equals   " + (go1.Equals(sb2)));
Run Code Online (Sandbox Code Playgroud)

我试图找出如何成功确定两个泛型类型值是否相等.根据Mark Byers对这个问题的回答,我认为我可以使用value.Equals()值是泛型类型的地方.我的实际问题是在LinkedList实现中,但问题可以通过这个更简单的示例来显示.

class GenericOjbect<T> {
    public T Value { get; private set; }
    public GenericOjbect(T value) {
        Value = value;
    }
    public bool Equals(T value) {
        return (Value.Equals(value));
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我定义一个GenericObject<StringBuilder>包含的实例new StringBuilder("StackOverflow").我希望得到true我来找Equals(new StringBuilder("StackOverflow")这个GenericObject实例,但我得到false.

一个示例程序显示:

using System;
using System.Text;

class Program
{
    static void Main()
    {
        var sb1 = new StringBuilder("StackOverflow");
        var sb2 = …
Run Code Online (Sandbox Code Playgroud)

c# generics comparison

10
推荐指数
2
解决办法
8336
查看次数

什么时候重载的false运算符会被执行,它有什么用呢?

我一直在寻找实际工作代码,其中重载false运算符实际上被执行.

这个问题(C#中的false运算符有什么用?)有点相同,但是接受的答案链接到一个返回404错误的url.我还看过运营商如何重载真假工作?和其他一些问题.

我几乎在所有答案中都发现,false只有当你使用短路时才能执行x && y.这被评估为T.false(x) ? x : T.&(x, y).

好的,所以我有以下代码.在struct包含int和认为自己真要是int是大于零:

public struct MyStruct {
    private int _i;

    public MyStruct(int i) {
        _i = i;
    }

    public static bool operator true(MyStruct ms) {
        return ms._i > 0;
    }

    public static bool operator false(MyStruct ms) {
        return ms._i <= 0;
    }

    public override string ToString() {
        return this._i.ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我希望以下程序将执行并使用重载false运算符.

class Program { …
Run Code Online (Sandbox Code Playgroud)

c# operator-overloading

10
推荐指数
2
解决办法
827
查看次数