小编Gre*_*reg的帖子

为什么Array类没有直接暴露其索引器?

要回答的事情:

  1. 不要担心差异,而有问题的项目则Array不是T[].

  2. 多维数组的类似情况是[ 这里 ]

也就是说,N-dims到线性变换总是可行的.所以这个问题引起了我的注意,因为它已经实现IList了线性索引器.


题:

在我的代码中,我有以下声明:

public static Array ToArray<T>(this T source); 
Run Code Online (Sandbox Code Playgroud)

我的代码知道如何使souce礼物成为一个数组(在运行时).我正试图让消费代码直接访问其索引器.但如果没有"作为IList",它就不可能完成. 要返回object[]可能需要额外的转换/转换,这就是我要阻止做的事情. 我能做的是:

public static IList ToArray<T>(this T source); 
Run Code Online (Sandbox Code Playgroud)

但我认为一个名为ToArrayreturn的方法IList看起来很奇怪.

因此,我对此感到困惑:

在声明中Array,有

object IList.this[int index];
Run Code Online (Sandbox Code Playgroud)

这样我们就可以

Array a;
a=Array.CreateInstance(typeof(char), 1);
(a as IList)[0]='a';
Run Code Online (Sandbox Code Playgroud)

但我们做不到

a[0]='a';
Run Code Online (Sandbox Code Playgroud)

除非它被宣布为

public object this[int index]; 
Run Code Online (Sandbox Code Playgroud)

我能看到的唯一区别是它需要我们通过IList实现它的接口显式地使用它的索引器,但为什么呢?有好处吗?或者是否存在暴露问题?

c# arrays ilist indexer

22
推荐指数
2
解决办法
3509
查看次数

DotNetNuke 7皮肤教程

我正在寻找一个关于为DotNetNuke 7创建皮肤的体面教程.我找不到任何最新版本的dnn,虽然我已经成功修改现有的皮肤,但它会很多更容易从头开始构建它们.

有什么建议?

c# asp.net dotnetnuke webforms

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

Visual Studio单页应用程序集成

技术:

  • Visual Studio 2017
  • Asp.Net核心工具1.1
  • .Net Framework 4.6.2

单页应用程序及其与Visual Studio的集成变得更加容易,Visual Studio中内置了所有新的支持.但在本周早些时候,Jeffery T. Fritz发布了一篇关于集成和使用以下软件包实现的非常好的文章:

  • Microsoft.AspNetCore.SpaServices
  • Microsoft.AspNetCore.NodeServices

在您完成并甚至分析几个模板后,您会在解决方案资源管理器中注意到一个名为的目录ClientApp.这是通过Webpack配置和路由的.

Startup.cs问题所在的内部.

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
            {
                HotModuleReplacement = true
            });
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");

            routes.MapSpaFallbackRoute(
                name: "spa-fallback",
                defaults: new { controller = "Home", action = "Index" });
        });
    }
Run Code Online (Sandbox Code Playgroud)

在我们的请求中,我们有一些路由到我们的MVC框架.

问题是,为什么我们需要指定这条路线? 如果我们只是使用app.UseDefaultFiles()app.UseStaticFiles()我们的wwwroot指定我们的索引.我们的客户端路由器将始终返回.那么为什么我们不这样做呢: …

javascript c# asp.net single-page-application asp.net-core

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

嘘到巧克力神

更新:

该实用程序是通过我正在经历的经过认证的Microsoft Visual Academy(MVA)视频来帮助配置Git.

当我通过Chocolatey安装软件包时,我收到一个特殊的错误.

  • 我上传了命令提示符
  • 指向Chocolatey Bin目录的目录
  • 尝试安装通过 cinst poshgit

它下载并显示为好像它正在工作,它甚至创建了目录C:\Tools\Poshgit.然后它给了我以下内容:

[错误]无法将参数绑定到参数'Path',因为它是一个空字符串.在C:\ Chocolatey\ChocolateyInstall\Helpers\functions\Writer-ChocolateyFailure.ps1:30 char 2

这导致了失败,我不完全确定为什么.这是一个全新的Chocolatey安装.难道我做错了什么?

c# git powershell command-line chocolatey

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

表情体VS块体

在编码时,出现了差异.通常在编写简单方法或构造函数时,我经常使用表达体技术.但是,当我生成以下内容时:

public class Sample : ISample
{
     private readonly IConfigurationRoot configuration;

     public Sample(IConfigurationRoot configuration) => this.configuration = configuration;
}
Run Code Online (Sandbox Code Playgroud)

代码似乎是有效的,Visual Studio和编译都工作.问题虽然来自同一个类,我去使用configuration变量.它产生"字段初始值设定项不能引用非静态字段初始值设定项".

产生的语法用法:

var example = configuration.GetSection("Settings:Key").Value;
Run Code Online (Sandbox Code Playgroud)

但是,如果我将片段留在此上方并修改为块体.Visual Studio不再吓坏了,为什么表达体会导致如此特殊的错误?块体与上面的片段一起正常工作吗?

public class Sample : ISample
{
     private readonly IConfigurationRoot configuration;

     public Sample(IConfigurationRoot configuration)
     {
           this.configuration = configuration;
     }
}
Run Code Online (Sandbox Code Playgroud)
public class ApplicationProvider
{
     public IConfigurationRoot Configuration { get; } = CreateConfiguration();

     public IServiceProvider BuildProvider()
     {
         var services = new ServiceCollection();
         DependencyRegistration(services);

         return services.AddLogging().BuildServiceProvider();
     }

     private IConfigurationRoot CreateConfiguration() => new …
Run Code Online (Sandbox Code Playgroud)

c# aspnet-compiler asp.net-core

7
推荐指数
0
解决办法
1328
查看次数

IDispose和最佳实践

我使用了代码分析工具,它提供了以下警告.

严重级代码描述项目文件行抑制状态警告CA2202对象'stream'可以在方法'Cipher.Encryptor(string)'中多次处理.为避免生成System.ObjectDisposedException,不应在对象上多次调用Dispose:Lines:43 Service Layer ...\Cipher.cs 43 Active

它源于"权力之塔":

    public static string Encryptor(string input)
    {
        var content = String.Empty;

        var cipher = new RijndaelManaged();
        var plain = Encoding.Unicode.GetBytes(input);
        var key = new PasswordDeriveBytes(password, salt);

        using (var encrypt = cipher.CreateEncryptor(key.GetBytes(32), key.GetBytes(16)))
        using (var stream = new MemoryStream())
        using (var cryptographic = new CryptoStream(stream, encrypt, CryptoStreamMode.Write))
        {
            cryptographic.Write(plain, 0, plain.Length);
            cryptographic.FlushFinalBlock();
            content = Convert.ToBase64String(stream.ToArray());
        }

        return content;
    }
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我利用MemoryStream,CryptoStreamICryptoTransform.为什么Visual Studio的代码分析会将此标记为警告?这来自Visual Studio 2015中内置的代码分析.

=================

这是一个较短的repro:

using …
Run Code Online (Sandbox Code Playgroud)

c#

6
推荐指数
0
解决办法
130
查看次数

DI的五个W,IoC,因为它让我的大脑爆炸

所以控制反转是一个模糊的描述,因此依赖注入成为新的定义.这是一个非常非常强大的解决方案,对于从未遇到过这种情况的人来说,可能同样令人困惑.

所以在我不想成为头灯的鹿的过程中,我读了一遍.我找到了几本很棒的书和在线帖子.但就像所有美好的事物一样,更多的问题出现而不是答案.

这个问题吸收了一个未知项目的变量,这些变量一旦被引入我的项目就会被实现.

解决方案:

public interface ISiteParameter
{
    Guid CustomerId { get; set; }
    string FirstName { get; set; }
    string LastName { get; set; }
    string Phone { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的注射器:

public interface IInjectSiteParameter
{
     void InjectSite(ISiteParameter dependant);
}
Run Code Online (Sandbox Code Playgroud)

然后我创建了这个:

public class SiteContent : IInjectSiteParameter
{
    private ISiteParameter _dependent;

    #region Interface Member:
    public void InjectSite(ISiteParameter dependant)
    {
        _dependant = dependant;
    }
    #endregion
}
Run Code Online (Sandbox Code Playgroud)

然后使用共享引用实现它作为Fed变量我创建了一个要实现的类,如:

public class SiteParameters : ISiteParameter …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection interface inversion-of-control

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

解码UTF问题?

我正在研究我的android项目,我有一个异国情调的问题让我发疯.我正在尝试将字符串转换为Utf-16Utf-8.我使用这段代码来实现它,但它给了我一个带有一些负面成员的数组!

Java代码:

String Tag="???";
String Value="";
try{
            byte[] bytes = Tag.getBytes("UTF-16");
            for(int i=0;i<bytes.length;i++){
            Value=Value+String.valueOf(bytes[i])+",";
        }
Run Code Online (Sandbox Code Playgroud)

数组成员:数组成员是[-1,-2,51,6,-52,6,49,6].我检查了UTF-16的表格.它没有任何负数,我也使用了一个将单词转换为UTF-16M的网站.它给了我"0633 06CC 0631"HEX.如果将此数字更改为十进制,您将看到:"1577 1740 1585".如你所见,这里没有负数!所以我的第一个问题是这些负数是什么?!

为什么我要将单词转换为UTF-8或UTF-16?

我正在做一个项目.这个项目有两个部分.第一部分是一个Android应用程序,它将关键字发送到服务器.这些单词由客户发送.我的客户使用(波斯语,فارسی)字符.第二部分是由C#制作的Web应用程序,它必须响应我的客户.

问题:当我将这些单词发送到服务器时,它会在"????"的流上运行 而不是正确的单词.我已经尝试了很多方法来解决这个问题,但他们无法解决这个问题.之后我决定将utf-16utf-8字符串自己发送到服务器并将其转换为正确的单词.所以我选择了我在帖子顶部描述的方法.

我的原始代码可靠吗?

是的.如果我使用英文字符,它反应非常好.

我的原始代码是什么?

将参数发送到服务器的Java代码:

    protected String doInBackground(String...Urls){
                String Data="";
                HttpURLConnection urlConnection = null; 
                try{
                    URL myUrl=new URL("http://10.0.2.2:80/Urgence/SearchResault.aspx?Tag="+Tag);
                    urlConnection = (HttpURLConnection)myUrl.openConnection();      
                    BufferedReader in = new BufferedReader (new InputStreamReader(urlConnection.getInputStream()));         
                    String temp=""; 
                    // Data is used to …
Run Code Online (Sandbox Code Playgroud)

c# java

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

神圣的反思

我收到一个错误,"无法将字符串转换为int?" .我觉得很奇怪,当你利用PropertyInfo.SetValue它时,我的想法确实应该尝试使用那种字段类型.

// Sample:
property.SetValue(model, null, null);
Run Code Online (Sandbox Code Playgroud)

以上将default(T)根据PropertyInfo.SetValueMicrosoft Developer Network 尝试在该属性上实现.但是,当我实现以下代码时:

// Sample:
property.SetValue(model, control.Value, null);
Run Code Online (Sandbox Code Playgroud)

错误泡沫,当我实现一个string应该具有的属性时int?,我认为它会尝试自动解析指定的类型.我如何帮助指定类型?

// Sample:
PropertyInfo[] properties = typeof(TModel).GetProperties();
foreach(var property in properties)
     if(typeof(TModel).Name.Contains("Sample"))
          property.SetValue(model, control.Value, null);
Run Code Online (Sandbox Code Playgroud)

任何澄清以及如何解决演员阵容都会有所帮助.为简洁起见,对示例进行了修改,尝试提供相关代码.

c# asp.net reflection asp.net-mvc

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

带有 SecureString 的配置实际上安全吗?

在我的一次代码审查中,我偶然发现了一个有趣的SecureString. 从逻辑上讲,将值隐藏在内存中是有好处的,但我的理解IConfiguration是,当通过ConfigurationBuilder副本注入和构建时,内存中已经存在以供使用。因此,SecureString虽然隐藏了明文值,但配置访问会自动否定密文。

我的想法是否正确,真的价值是不安全的,甚至不应该使用,SecureString因为它开始不安全 -

public class Sample
{
     private readonly SecureString secret;
     public Sample(IConfiguration configuration) => secret = new NetworkCredentials(String.Empty,
          configuration.GetSection("Api:Credentials")["secret"]).SecurePassword;
}
Run Code Online (Sandbox Code Playgroud)

c# .net-core

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