小编Sco*_*ain的帖子

是否有命令行命令用于验证安装的.NET版本

我有一组用于执行脚本安装的脚本.您可以在任何服务器2008计算机上使用这些脚本.但是,我需要使用dos批处理文件检查是否已安装.NET 3.5(在脚本运行之前).那可能吗?

我知道我可以检查文件是否C:\WINDOWS\Microsoft.NET\Framework\v3.5存在,但是让某些东西更可靠会更好.

我想检查它是否实际安装,而不仅仅是目录/文件是否存在.

谢谢

.net windows cmd batch-file

63
推荐指数
4
解决办法
15万
查看次数

如果使用具有大对象的枚举,Parallel.ForEach可能会导致"内存不足"异常

我正在尝试将数据库中存储图像的数据库迁移到指向硬盘驱动器上的文件的数据库中的记录.我试图使用此方法来查询数据Parallel.ForEach以加快进程.

但是,我注意到我得到了一个OutOfMemory例外.我知道Parallel.ForEach会查询一批枚举,以减少开销的成本,如果有一个用于间隔查询(如果您一次执行一堆查询而不是间隔它们,您的源将更有可能将下一条记录缓存在内存中)出).问题是由于我返回的记录之一是1-4Mb字节数组,缓存导致整个地址空间用完(程序必须在x86模式下运行,因为目标平台将是32位机)

是否有任何方法可以禁用缓存或使TPL更小?


这是一个显示问题的示例程序.这必须在x86模式下编译,以显示问题,如果它在你的机器上花费很长时间或者没有发生,从而增加了阵列的大小(我发现1 << 20我的机器上大约需要30秒,4 << 20几乎是瞬间的)

class Program
{

    static void Main(string[] args)
    {
        Parallel.ForEach(CreateData(), (data) =>
            {
                data[0] = 1;
            });
    }

    static IEnumerable<byte[]> CreateData()
    {
        while (true)
        {
            yield return new byte[1 << 20]; //1Mb array
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# out-of-memory large-data task-parallel-library

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

如何在文档注释中引用数组类型

我刚刚发布了这个问题并了解了<see cref="">,但是当我尝试的时候

/// This is a set of extensions that allow more operations on a <see cref="byte[]"/>.
Run Code Online (Sandbox Code Playgroud)

编译器给了我关于它没有正确格式化的警告.我需要做什么才能正确看到代码引用?

.net c# documentation comments visual-studio-2008

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

将Nullable <DateTime>序列化为XML

我试图序列化一个类的几个数据成员是Nullable对象,这里是一个例子

[XmlAttribute("AccountExpirationDate")]
public Nullable<DateTime> AccountExpirationDate 
{ 
  get { return userPrincipal.AccountExpirationDate; } 
  set { userPrincipal.AccountExpirationDate = value; } 
}
Run Code Online (Sandbox Code Playgroud)

但是在运行时我得到了错误

无法序列化System.Nullable`1 [System.DateTime]类型的成员'AccountExpirationDate'.XmlAttribute/XmlText不能用于编码复杂类型.

但是我检查过Nullable是一个SerializableAttribute.我究竟做错了什么?

c# nullable xml-serialization

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

使用LINQ的.Cast()运算符时,显式/隐式转换运算符失败

我试图使用一个具有显式(但也失败的隐式)转换操作符的类,该操作符在使用LINQ Cast<T>()函数时失败.以下是这两个类的定义

public class DatabaseInfoElement : ConfigurationElement
{
    [ConfigurationProperty("AllowedServer", IsRequired = true)]
    public string AllowedServer { get { return (string)base["AllowedServer"]; } }

    [ConfigurationProperty("DatabaseName", IsRequired = true)]
    public string DatabaseName { get { return (string)base["DatabaseName"]; } }

    [ConfigurationProperty("SqlInstance", IsRequired = true)]
    public string SqlInstance { get { return (string)base["SqlInstance"]; } }

    public static explicit operator DatabaseInfo(DatabaseInfoElement element)
    {
        return new DatabaseInfo(element.AllowedServer, element.DatabaseName, element.SqlInstance);
    }

}

public class DatabaseInfo
{
    public DatabaseInfo(string allowedServer, string sqlInstance, string databaseName)
    {
        AllowedServer = allowedServer; …
Run Code Online (Sandbox Code Playgroud)

c# linq casting exception

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

将两位数年份转换为四位数年份

这是最佳实践的问题.我有一个实用程序,它将一个两位数的年份作为字符串,我需要将其转换为四位数年份作为字符串.现在我做

//DOB's format is "MMM (D)D YY" that first digit of the day is not there for numbers 1-9
string tmpYear = rowIn.DOB.Substring(rowIn.DOB.Length - 3, 2); //-3 because it is 0 indexed
if (Convert.ToInt16(tmpYear) > 50)
    tmpYear = String.Format("19{0}", tmpYear);
else
    tmpYear = String.Format("20{0}", tmpYear);
Run Code Online (Sandbox Code Playgroud)

我确信我做错了,任何指针?

c# string date

29
推荐指数
5
解决办法
4万
查看次数

AutomationElement使用Inspect.exe显示,但在使用UIAutomationCore.dll或System.Windows.Automation时显示不亮.

TL; DR:我做错了什么导致工作区窗格显示在Inspect Objects中但未显示在我的自定义代码中?


我正在尝试为第三方程序编写一些UI自动化.我正在使用Windows SDK附带的Inspect.exe,我已经尝试过System.Windows.Automation和直接COM调用(使用UIA Verify的包装器库).

Process[] processes = Process.GetProcessesByName("Redacted Client");
if (processes.Length == 0) throw new Exception("Could not find \"Redacted Client\" process");

PropertyCondition parentFileCond = new PropertyCondition(AutomationElement.ProcessIdProperty, processes[0].Id);
PropertyCondition workspaceCond = new PropertyCondition(AutomationElement.NameProperty, "Workspace", PropertyConditionFlags.IgnoreCase);
PropertyCondition documentCond = new PropertyCondition(AutomationElement.NameProperty, "Untitled3", PropertyConditionFlags.IgnoreCase);

var parentElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, parentFileCond);
var workspaceElement = parentElement.FindFirst(TreeScope.Children, workspaceCond); //Also does not work with TreeScope.Descendants
var documentElement = workspaceElement.FindFirst(TreeScope.Children, documentCond);
Run Code Online (Sandbox Code Playgroud)

当我尝试上面的代码时,parentElement确实有对主程序窗口的正确引用,但是workspaceElement为null. …

.net c# automation ui-automation

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

处理ThreadLocal <IDisposable>中保存的元素的正确方法是什么?

当你使用a ThreadLocal<T>和T实现IDisposable时,你应该如何处理ThreadLocal中的成员?

根据ILSpy,ThreadLocal的Dispose()和Dispose(bool)方法是

public void Dispose()
{
    this.Dispose(true);
    GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
    int currentInstanceIndex = this.m_currentInstanceIndex;
    if (currentInstanceIndex > -1 && Interlocked.CompareExchange(ref this.m_currentInstanceIndex, -1, currentInstanceIndex) == currentInstanceIndex)
    {
        ThreadLocal<T>.s_availableIndices.Push(currentInstanceIndex);
    }
    this.m_holder = null;
}
Run Code Online (Sandbox Code Playgroud)

ThreadLocal似乎没有尝试在其子成员上调用Dispose.我不知道如何引用它内部分配的每个线程,所以我可以处理它.


我使用以下代码运行测试,该类从未处理过

static class Sandbox
{
    static void Main()
    {

        ThreadLocal<TestClass> test = new ThreadLocal<TestClass>();
        test.Value = new TestClass();

        test.Dispose();
        Console.Read();
    }
}

class TestClass : IDisposable
{
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    protected void Dispose(bool Disposing)
    {
        Console.Write("I …
Run Code Online (Sandbox Code Playgroud)

c# idisposable .net-4.0 thread-local

25
推荐指数
2
解决办法
4967
查看次数

没有System.Runtime.Caching可用?

"类库"的输出类型,以及".NET Framework 4"的目标框架.

根据我读过的所有内容,我应该可以使用它,但是我在System.Runtime命名空间中看到的所有内容如下:

CompilerServices
ConstrainedExecution
ExceptionServices
Hosting
InteropServices
Remoting
Serialization
Versioning
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

.net c# visual-studio-2012

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

定时器可以自动收集垃圾吗?

当您使用a Timer或者Thread只运行程序的整个生命周期时,您是否需要保留对它们的引用以防止它们被垃圾回收?

请放下以下程序可以timer作为类中的静态变量的事实,这只是一个展示问题的玩具示例.

public class Program
{
    static void Main(string[] args)
    {
        CreateTimer();

        Console.ReadLine();
    }

    private static void CreateTimer()
    {
        var program = new Program();

        var timer = new Timer();
        timer.Elapsed += program.TimerElapsed;
        timer.Interval = 30000;
        timer.AutoReset = false;
        timer.Enabled = true;
    }

    private void TimerElapsed(object sender, ElapsedEventArgs e)
    {
        var timerCast = (Timer)sender;

        Console.WriteLine("Timer fired at in thread {0}", GetCurrentThreadId());

        timerCast.Enabled = true;
    }

    ~Program()
    {
        Console.WriteLine("Program Finalized");
    }

    [DllImport("kernel32.dll")]
    static extern uint GetCurrentThreadId(); …
Run Code Online (Sandbox Code Playgroud)

c# multithreading garbage-collection timer

24
推荐指数
2
解决办法
6833
查看次数