小编Dan*_*ker的帖子

声明变量,类型为'Type'类型的变量

我的班级中有变量类型为'Type':

class A
{
    public static Type MyType;
}
Run Code Online (Sandbox Code Playgroud)

我在任何地方都建造了这个变量.例:

A.MyType = b.GetType();
Run Code Online (Sandbox Code Playgroud)

第一个问题:我需要声明存储在MyType中的类型的新变量.

<MyType> myValue = new <MyType>;
Run Code Online (Sandbox Code Playgroud)

第二个问题:我需要一个函数来指示是否可以减少'Type'的其他变量.

.net c#

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

C# - 在catch子句中使用return

我在这里看到了类似的问题,但仍有一些我不明白的事情.据我所知,当你使用try-catchblock时,如果抛出异常,catch块将立即执行,并且不会执行catch相同代码块中的子句之后的代码.所以,如果我做到了,如果我们有:

try
{
    // do something
    // throw an exception for some reason
}
catch (Exceptiox ex)
{
    // do something when there is and exception thrown
}
// some code that will never be runned if and exception was thrown above
Run Code Online (Sandbox Code Playgroud)

我不是100%确定catch停止在其范围之外进一步执行,但这是我的一个问题,所以如果我错了,请纠正我.

那么,如果你根本不需要返回任何值,那么return在catch块中使用的重点是什么?我在一些方法中看到这个来自我正在研究的继承代码.例如:

public void DeleteImage(AppConfig imageInfo, string imageName)
{
    string imgPath = imageInfo.ConfigValue.ToString();
    try
    {
        File.Delete(imgPath + "\\" + imageName);
    }
    catch (Exception ex)
    {
        logger.Error(ex.ToString());
        return;
    } …
Run Code Online (Sandbox Code Playgroud)

.net c# try-catch

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

编译器没有绑定到正确的泛型方法重载

通常,C#编译器对方法绑定和类型参数推断很聪明.但我似乎已经难倒了.

class Obj
{
    void Handler( int a, int b ) { }

    Obj() { Method( "", Handler ); }

    public void Method<T>( T t, Action<T> action ) { }

    public void Method<T, U>( T t, Action<U, U> action ) { }
}
Run Code Online (Sandbox Code Playgroud)

该Method调用导致编译器错误:

参数2:无法从'方法组'转换为'System.Action'.

为什么编译器没有注意到调用符合第二个重载?我可以通过使调用更明确,如在Method<string, int>( "", Handler )或中编译它Method( "", (Action<int, int>)Handler ).但为什么这有必要呢?

c# overloading type-inference compiler-errors

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

如何将“0.0000e+000”解析为浮点数/双精度数?

我尝试了以下方法:

string val = "0.0000e+000";
float.Parse(val);
Run Code Online (Sandbox Code Playgroud)

...还得到了一个FormatException. 所以我想知道:如何将这样的值解析为浮点/双精度?

解析为浮点数

解析为双精度数

.net c#

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

将日期转换为DOS日期

我遇到了DOS日期格式的问题.我需要转换:

From:
       29th June of 2011 
To:
       16093

我知道16093是正确的结果,但我怎样才能得到这个结果?


我可以将DOS日期整数值转换为已识别DateTime但不知道如何反转该过程.这是我如何从DOS日期转换为DateTime:

var number = 16093;

var year = (number >> 9) + 1980;
var month = (number & 0x01e0) >> 5;
var day = number & 0x1F;
var date = new DateTime((int)year, (int)month, (int)day);
Run Code Online (Sandbox Code Playgroud)

这有效.现在我需要扭转它.

c# datetime dos

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

将Float转换为Int

所以我有一个我正在研究的项目.这是我唯一的错误:

无法将类型'float'隐式转换为'int'.

我理解这意味着什么.我只需要帮助将我的浮点数转换为int.

这只是其中一个花车的一个例子:

float key = 0.5f;
int key = 53;
Run Code Online (Sandbox Code Playgroud)

这是具体的代码部分:

// price in scrap, e.g. 29 / 9 = 3.33 ref
static int BuyPricePerTOD = 21;
// price in scrap, e.g. 31 / 9 = 3.55 ref
static float SellPricePerTOD = BuyPricePerTOD + 0.5F;

static int BuyPricePerKey = 53;
static float SellPricePerKey = BuyPricePerKey + 0.5F;

static int TimerInterval = 170000;
static int InviteTimerInterval = 2000;

int UserWeapAdded,UserScrapAdded,UserRecAdded,UserRefAdded,
    UserKeysAdded,UserTODAdded,BotTODsAdded,BotKeysAdded,
    BotScrapAdded,BotRecAdded,BotRefAdded,InventoryMetal,
    InventoryScrap,InventoryRec,InventoryRef,InventoryKeys,
    InventoryTOD,PreviousTODs,PreviousKeys,WhileLoop,InvalidItem = 0;

float UserMetalAdded, BotMetalAdded, …
Run Code Online (Sandbox Code Playgroud)

c#

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

如何复制文件并覆盖现有文件

我有两个名为Load和New 的按钮。首先,您必须加载文件。然后,当您单击“新建”按钮时,它必须复制您加载的文件,覆盖它,并同时修改上次修改日期。我怎样才能做到这一点?我尝试了下面的代码,但没有任何反应。

我在“新建”按钮下复制文件的代码:

FileInfo originalfile = new FileInfo(PathSelection); 
FileInfo newfile = new FileInfo(PathSelection); 
Run Code Online (Sandbox Code Playgroud)

c# copy file

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

如何检测文件是否存在?

任何人都可以向我建议查看文件是否存在的最佳方法吗?File.Exists不适合我.

string abc = "me_label.deploy";
File.Exists(abc)
Run Code Online (Sandbox Code Playgroud)

该文件abc来自Streamreader.ReadLine();.

我甚至使用了完整的路径.我不想在我的项目中包含文件.如果我把它包含在我的项目中它工作正常.

我的代码:

FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("me@localhost/"; + ab);
//ab=newfolder/newfolder1
ftp.Credentials = new NetworkCredential(user, pass);
ftp.Method = WebRequestMethods.Ftp.ListDirectory;
StreamReader sr = new StreamReader(ftp.GetResponse().GetResponseStream());
StringBuilder result = new StringBuilder();
string abc = sr.ReadLine();
while (abc != null)
{
    result.Append(abc);
    //abc=file
    result.Append("\n");
    if (File.Exists(ab+abc))
    {
        //file
    }
} 
Run Code Online (Sandbox Code Playgroud)

c# ftp file

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

标签 统计

c# ×8

.net ×3

file ×2

compiler-errors ×1

copy ×1

datetime ×1

dos ×1

ftp ×1

overloading ×1

try-catch ×1

type-inference ×1