走过海棠暮

文章 评论 浏览 25

走过海棠暮 2024-09-05 02:46:25

下面是它的扩展方法示例:

public static List<int> AllIndexesOf(this string str, string value) {
    if (String.IsNullOrEmpty(value))
        throw new ArgumentException("the string to find may not be empty", "value");
    List<int> indexes = new List<int>();
    for (int index = 0;; index += value.Length) {
        index = str.IndexOf(value, index);
        if (index == -1)
            return indexes;
        indexes.Add(index);
    }
}

如果将其放入静态类中并使用 using 导入命名空间,则它会显示为任何字符串上的方法,您可以执行以下操作:

List<int> indexes = "fooStringfooBar".AllIndexesOf("foo");

有关扩展的更多信息方法,http://msdn.microsoft.com/en-us/library/bb383977 .aspx

使用迭代器也一样:

public static IEnumerable<int> AllIndexesOf(this string str, string value) {
    if (String.IsNullOrEmpty(value))
        throw new ArgumentException("the string to find may not be empty", "value");
    for (int index = 0;; index += value.Length) {
        index = str.IndexOf(value, index);
        if (index == -1)
            break;
        yield return index;
    }
}

Here's an example extension method for it:

public static List<int> AllIndexesOf(this string str, string value) {
    if (String.IsNullOrEmpty(value))
        throw new ArgumentException("the string to find may not be empty", "value");
    List<int> indexes = new List<int>();
    for (int index = 0;; index += value.Length) {
        index = str.IndexOf(value, index);
        if (index == -1)
            return indexes;
        indexes.Add(index);
    }
}

If you put this into a static class and import the namespace with using, it appears as a method on any string, and you can just do:

List<int> indexes = "fooStringfooBar".AllIndexesOf("foo");

For more information on extension methods, http://msdn.microsoft.com/en-us/library/bb383977.aspx

Also the same using an iterator:

public static IEnumerable<int> AllIndexesOf(this string str, string value) {
    if (String.IsNullOrEmpty(value))
        throw new ArgumentException("the string to find may not be empty", "value");
    for (int index = 0;; index += value.Length) {
        index = str.IndexOf(value, index);
        if (index == -1)
            break;
        yield return index;
    }
}

在 C# 中查找较大字符串中子字符串的所有位置

走过海棠暮 2024-09-04 19:54:34

Silverlight-Controlkit 附带了一个非常方便的“忙碌指示器”控件...太糟糕了,默认情况下 WPF 似乎没有这样的东西。

但我发现这对你来说似乎是相当的控制:
http:// /sweux.com/blogs/pombeiro/index.php/2009/12/01/a-busy-state-indicator-attached-behavior/

下载源:
http://gallery.expression.microsoft.com/en-us/BusyIndi​​cator

Silverlight-Controlkit comes with a very handy "busyindicator"-control... too bad there seems to be no such thing for WPF by default.

But I found this seemfully comparable control for you:
http://sweux.com/blogs/pombeiro/index.php/2009/12/01/a-busy-state-indicator-attached-behavior/

download-source:
http://gallery.expression.microsoft.com/en-us/BusyIndicator

ac# wpf 列表框的加载屏幕

走过海棠暮 2024-09-04 16:57:23

另一种选择是从控制器操作返回格式化字符串。您甚至可以保留时间戳并返回第二个字段作为“格式化时间戳”或类似的内容。

var listFromDb = ...
return new Json(listFromDb.Select(itemFromDb => new List { new 
     { Date = itemFromDb.Date, FormattedDate = FormatDate(itemFromDb.Date), ...}

The other alternative is to return the formatted string from the controller action. You could even leave the timestamp and return a second field as "Formatted Timestamp" or something similar.

var listFromDb = ...
return new Json(listFromDb.Select(itemFromDb => new List { new 
     { Date = itemFromDb.Date, FormattedDate = FormatDate(itemFromDb.Date), ...}

使用 javascript/jquery 从数据库格式化日期的正确方法

走过海棠暮 2024-09-04 13:54:30

该函数在 text.module 中定义。与 hook_theme() 实现返回的条目 text_formatter_default 匹配的主题函数theme_text_formatter_default()`。

/**
 * Theme function for 'default' text field formatter.
 */
function theme_text_formatter_default($element) {
  return ($allowed =_text_allowed_values($element)) ? $allowed : $element['#item']['safe'];
}

The function is defined in text.module. the theme function that matches the entry text_formatter_default returned from the implementation of hook_theme()istheme_text_formatter_default()`.

/**
 * Theme function for 'default' text field formatter.
 */
function theme_text_formatter_default($element) {
  return ($allowed =_text_allowed_values($element)) ? $allowed : $element['#item']['safe'];
}

对主题函数调用感到困惑

走过海棠暮 2024-09-04 12:42:57

一定要安装免费数据库之一(Oracle Express、SQL Server Express)。这两个我都用过。您可能还想查看此免费教程。它将帮助您学习 SQL 语法。

Definitely install one of the free databases (Oracle Express, SQL Server Express). I have used both of those. You might also want to check out this free tutorial. It will help you learn SQL syntax.

从 SQL 开始

走过海棠暮 2024-09-04 09:45:13

我确信在某些情况下,用一种 .NET 语言编写的惯用代码的性能会比用另一种 .NET 语言编写的性能稍高一些。然而,退一步来说,为什么这很重要呢?您心中有绩效目标吗?即使在单一语言中,您也经常可以做出影响性能的选择,有时您需要在性能与可维护性或开发时间之间进行权衡。如果您没有构成可接受性能的目标,则无法评估语言之间的任何性能差异是否有意义或可以忽略不计。

此外,编译器不断发展,因此今天的相对性能不一定适用于未来。 JIT 编译器也在不断发展。即使处理器设计也是可变且不断发展的,因此相同的 JITTed 本机代码可以在具有不同缓存层次结构、管道大小、分支预测等的处理器上以不同的方式执行。

话虽如此,可能有一些在很大程度上适用的广泛规则:

  1. 算法差异可能会比编译器差异产生更大的差异(至少在比较在 CLR 上运行的静态类型语言时)
  2. 对于可以轻松并行化的问题,可以轻松利用多个处理器/内核的语言将提供加速代码的简单方法。

I'm sure that there are scenarios where idiomatic code is slightly more performant when written in one .NET language than another. However, stepping back a bit, why does it matter? Do you have a performance target in mind? Even within a single language there are often choices which you can make that affect performance, and you will sometimes need to trade performance off against maintainability or development time. If you don't have a target for what constitutes acceptable performance, then it's impossible to evaluate whether any performance differences between languages are meaningful or negligible.

Additionally, compilers evolve, so what's true of the relative performance today won't necessarily hold going forward. And the JIT compiler is evolving too. Even processor designs are variable and evolving, so the same JITTed native code can perform differently across processors with different cache hierarchies, pipeline sizes, branch prediction, etc.

Having said all of that, there are probably a few broad rules that largely hold true:

  1. Algorithm differences are probably going to make a bigger difference than compiler differences (at least when comparing statically typed languages running on the CLR)
  2. For problems which can be parallelized easily, languages which make it easy to take advantage of multiple processors/cores will provide a simple way to speed up your code.

.net 中使用的所有语言都具有相同的性能吗?

走过海棠暮 2024-09-04 09:34:47

我有类似的经历,但这与双字节有更多关系。我有 2 个 char* 定义背对背。我将一些字符读入第一个字符串。事实证明这是双字节,因此字符串的其余部分溢出到第二个字符串。

I have a similar experience but this has more to do with double byte. I have 2 char* define back to back. I read some char into the first string. Turns out that is was double byte, so the remaining of the string spill over to the second string.

C 标准输出 printf

走过海棠暮 2024-09-04 06:46:54

请注意,您使用 Exists() 来检查正在使用的文件或目录名称这一事实会受到竞争条件的影响。

例如,在您的 Exists() 测试通过后的任何时候,在您的代码到达您创建文件的位置之前,某些东西可能已经创建了具有该名称的文件。

(我假设这是文件已经存在的特殊情况)。

更可靠的方法是简单地打开文件并指定适当的 FileShare 参数。

示例:

using System;
using System.IO;

static class FileNameInUse
{
    static void Main(string[] args)
    {
        string path = args[0];
        using (var stream = File.Open(path, FileMode.CreateNew, FileAccess.Write, FileShare.None))
        {
            // Write to file
        }
    }
}

因此,简单地处理失败时的 IOException 可能会导致更简单的代码,不易出现竞争条件,因为现在:

  • 如果其他东西已经创建了该文件,FileMode.CreateNew 将导致抛出IOException
  • 如果您的打开和创建成功,由于FileShare.None,在您关闭该文件之前,没有其他进程可以访问该文件。

不幸的是,在没有一些丑陋的 P/Invoke 的情况下,不可能检查文件当前是否正在使用,并且不抛出异常:

    bool IsFileInUse(string fileName)
    {
            IntPtr hFile = Win32.CreateFile(fileName, Win32.FILE_READ_DATA, 0, IntPtr.Zero, Win32.OPEN_EXISTING, Win32.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
            if (hFile.ToInt32() == Win32.INVALID_HANDLE_VALUE)
                return true;

            Win32.CloseHandle(hFile);
            return false;
    }

    class Win32
    {
        const uint FILE_READ_DATA = 0x0001;
        const uint FILE_SHARE_NONE = 0x00000000;
        const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
        const uint OPEN_EXISTING = 3;
        const int INVALID_HANDLE_VALUE = -1;

        [DllImport("kernel32.dll", SetLastError=true)]
        internal static extern IntPtr CreateFile(string lpFileName,
                                               uint dwDesiredAccess,
                                               uint dwShareMode,
                                               IntPtr lpSecurityAttributes,
                                               uint dwCreationDisposition,
                                               uint dwFlagsAndAttributes,
                                               IntPtr hTemplateFile);

        [DllImport("kernel32.dll")]
        internal static extern bool CloseHandle(IntPtr hObject);
    }

而且这种快速检查也容易出现竞争条件,除非您返回从中获取文件句柄,并将其传递给相关的 FileStream 构造函数。

Note that the fact that you are using Exists() to check for file or directory name in use is subject to race conditions.

At any point after your Exists() test has passed, something could have created a file with that name before your code reaches the point where you create a file, for example.

(I'm assuming it is an exceptional condition for the file to already exist).

It is more reliable to simply to open the file, specifying an appropriate FileShare parameter.

Example:

using System;
using System.IO;

static class FileNameInUse
{
    static void Main(string[] args)
    {
        string path = args[0];
        using (var stream = File.Open(path, FileMode.CreateNew, FileAccess.Write, FileShare.None))
        {
            // Write to file
        }
    }
}

So simply handling the IOException on failure may result in simpler code less prone to race conditions, because now:

  • If something else has already created the file, FileMode.CreateNew will cause an IOException to be thrown
  • If your open and create succeeds, because of FileShare.None, no other process can access the file until you close it.

Unfortunately, it is not possible to check whether a file is currently in use, and not throw an exception, without some ugly P/Invoke:

    bool IsFileInUse(string fileName)
    {
            IntPtr hFile = Win32.CreateFile(fileName, Win32.FILE_READ_DATA, 0, IntPtr.Zero, Win32.OPEN_EXISTING, Win32.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
            if (hFile.ToInt32() == Win32.INVALID_HANDLE_VALUE)
                return true;

            Win32.CloseHandle(hFile);
            return false;
    }

    class Win32
    {
        const uint FILE_READ_DATA = 0x0001;
        const uint FILE_SHARE_NONE = 0x00000000;
        const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
        const uint OPEN_EXISTING = 3;
        const int INVALID_HANDLE_VALUE = -1;

        [DllImport("kernel32.dll", SetLastError=true)]
        internal static extern IntPtr CreateFile(string lpFileName,
                                               uint dwDesiredAccess,
                                               uint dwShareMode,
                                               IntPtr lpSecurityAttributes,
                                               uint dwCreationDisposition,
                                               uint dwFlagsAndAttributes,
                                               IntPtr hTemplateFile);

        [DllImport("kernel32.dll")]
        internal static extern bool CloseHandle(IntPtr hObject);
    }

And this fast check is also prone to race conditions, unless you return the file handle from it, and pass that to the relevant FileStream constructor.

检查文件/目录是否存在:有更好的方法吗?

走过海棠暮 2024-09-04 01:01:31

指针算术使用基础类型的大小。如果 int 为 4 个字节:

int *p = some_address;
p++; 

将使 p 增加 4 个字节。 void 不能用于指针算术,因为 void 没有与之关联的大小。如果要对 void 指针执行字节大小的算术,则需要将指针转换为字节大小的类型。

Pointer arithmetic uses the size of the underlying type. If int is 4 bytes:

int *p = some_address;
p++; 

will increment p by 4 bytes. void can't be used in pointer arithmetic because void has no size associated with. If you want to do byte-sized arithmetic on void pointers, you need to cast the pointers to a byte-sized type.

关于指针运算的问题

走过海棠暮 2024-09-03 21:51:27

答案如下:
ASP.Net:使用系统共享/静态函数中的.Web.UI.Control.ResolveUrl()

string absoluteUrl = VirtualPathUtility.ToAbsolute("~/SomePage.aspx");

Here's the answer:
ASP.Net: Using System.Web.UI.Control.ResolveUrl() in a shared/static function

string absoluteUrl = VirtualPathUtility.ToAbsolute("~/SomePage.aspx");

如何解析 ASP.NET“~”没有控件的情况下应用程序到网站根目录的路径?

走过海棠暮 2024-09-03 17:25:42

您正在运行的安装程序正在检查 .Net 1.1 是否安装在注册表中(官方方式),而不是查看 C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322 是否存在文件夹。

因此,您可能没有安装 .Net 1.1,并且该文件夹是由一些工具创建的,这些工具添加了一些支持该 .Net 版本所需的文件,而无需检查它是否已安装(理由是如果没有安装,但稍后安装完毕后,他们就会开始工作)。

请注意,某些应用程序完全需要 .Net 1.1,并且无法在更高版本上运行(因为 1.1 和 2.0 之间存在某些重大更改)。

要解决没有 1.1 的实际问题,您可以获取它的安装程序来自微软。请注意,如果您在该计算机上使用 ASP.NET,则可能需要在安装 1.1 后重新注册 ASP.NET 3.5 并配置 IIS 才能使用它。

The setup you are running is checking whether the .Net 1.1 is installed in the registry (which is the official way), instead of looking at the presence of the C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322 folder.

So you probably don't have .Net 1.1 installed and the folder is created by some tools that add some files they need to support that .Net version without checkin if it is installed or not (with the rationale that if it's not, but later on it is installed, they'll just start working for it).

Note that certain applications require exactly .Net 1.1 and can't run on later versions (because of certain breaking changes between 1.1 and 2.0).

And to solve your actual problem of not having 1.1, you can get the installer for it from Microsoft. Note that if you are using ASP.NET on that machine, you might have to re-register ASP.NET 3.5 and configure IIS to use it after you install 1.1.

安装需要 .NET Framework v1.1.4322 - 但我已经拥有所有框架 1.0、1.1、2.0、3.0 和 3.5!

走过海棠暮 2024-09-03 15:55:32

使用您需要上传的每个文件的路径创建一个 CSV 文件。
发出一个从 csv 文件上传文件的请求。
创建一个 FOREACH 控制器来为 CSV 文件中的每一行发出上传请求。

Create a CSV file with the PATH for each file you need to upload.
Make ONE request that uploads a file from the csv file.
Create a FOREACH controller to make the upload request for each row in the CSV file.

jmeter和多个http请求

走过海棠暮 2024-09-03 14:13:33

如果您使用 MVVM light 工具包,您可以使用 Extras dll 中 Galasoft.MvvmLight.Threading 命名空间中的 DispatcherHelper 类。它检查访问并使用调度程序的静态属性,类似于 SmartDispatcher。

在您的 App.xaml.cs 启动事件调用中:

DispatcherHelper.Initialize();

然后在您需要使用调度程序的任何地方使用:

   DispatcherHelper.CheckBeginInvokeOnUI(() => // do stuff; );

If you use the MVVM light toolkit you could use the DispatcherHelper class in the Galasoft.MvvmLight.Threading namespace in the Extras dll. It checks the access and uses a static property for the dispatcher, similar to the SmartDispatcher.

In your App.xaml.cs startup event call:

DispatcherHelper.Initialize();

Then anywhere you need to use a dispatcher use:

   DispatcherHelper.CheckBeginInvokeOnUI(() => // do stuff; );

了解 Silverlight 调度程序

走过海棠暮 2024-09-03 11:52:28

事实上,解决方案确实与保留有关,正如用户不变所指出的那样。类方法:

output = [NSMutableString stringWithCapacity:0];

返回一个autorelease NSMutableString。当分配给我的输出属性时——看起来,即使有保留标志——它也没有保留它。解决方案是自己分配它而不是自动释放:

output = [[NSMutableString alloc] initWithCapacity:0];

然后保留就起作用了。任何关于原因的解释都将非常受欢迎。

编辑

找出原因。我直接访问实例变量,而不是通过我合成的 getter/setter。有关更多信息,请访问我的博客

The solution did in fact have to do with retention, as indicated by user invariant. The class method:

output = [NSMutableString stringWithCapacity:0];

returns an autorelease NSMutableString. When assigned to my output property -- seemingly, even with the retain flag -- it did not retain it. The solution was to alloc it myself and not autorelease:

output = [[NSMutableString alloc] initWithCapacity:0];

Then the retain worked. Any explanation as to why would be very welcome.

Edit

Figured out why. I was accessing the instance vars directly instead of through the getter/setter that I synthesized. More info on my blog.

将字符串附加到 NSMutableString

走过海棠暮 2024-09-03 06:04:12

坐下来思考如何在给定数组的起始位置的情况下找到 a[i][j] 的内存位置。

请注意,c 数组的布局如下 a[0][0] a[0][1] a[0][2] > ... a[0][M] a[1][0]...

旁注: FORTRAN 数组的布局不同的是: a[1][1] a[2][1] a[3][1] ... a[N][1] a[1][2] ...

注意这将如何改变查找内存位置所需的维度。

Sit down and figure out how to find the memory position of a[i][j] given the starting position of the array.

Note that c arrays are laid out like this a[0][0] a[0][1] a[0][2] ... a[0][M] a[1][0]...

Side note: FORTRAN arrays are laid out differently: a[1][1] a[2][1] a[3][1] ... a[N][1] a[1][2] ...

Notice how this would change which dimension is needed for finding memory positions.

为什么只有第二个数组维度很重要?

我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文