小编Mar*_*tin的帖子

从ConfigurationElementCollection获取配置元素

我(希望)设置我自己的设计的ConfigurationElementCollection,电子邮件作为键.怎么办?很难在网上找到.我如何能:

  1. 通过它迭代?

  2. 查看是否存在特定元素?

  3. 得到一个特定的元素?

...给出:

    YourConfigElement config = 
   ConfigurationManager.GetSection("YourSectionName") as YourConfigElement;
Run Code Online (Sandbox Code Playgroud)

部分答案

1.

 foreach (X x in config.XCollection) 
             <code here>
Run Code Online (Sandbox Code Playgroud)

2.用"替换此处的代码"代替

 { 
    if (x.Y == needle) 
    {
       hasIndeed = true;
       break;
    }
 }
Run Code Online (Sandbox Code Playgroud)

3.用"替换此处的代码"代替

 { if (x.Y == needle) 
       cameUpWith = x;
       break;
 }
Run Code Online (Sandbox Code Playgroud)

微小的气味.

c# system.configuration configurationelement

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

HandleErrorAttribute不起作用

我在VS10中启动了一个MVC 3模板项目,并修改了global.asax.cs:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute { ExceptionType = typeof(DivideByZeroException), View = "DivideByZeroException", Order = 1 });
    filters.Add(new HandleErrorAttribute { View = "AllOtherExceptions", Order = 2 });
}
Run Code Online (Sandbox Code Playgroud)

到web.config我添加了:

<customErrors mode="On">
Run Code Online (Sandbox Code Playgroud)

然后创建了相应的视图,最后在其中一个动作中添加了DivideByZero-throw.

结果:呈现了视图AllOtherExceptions.

asp.net-mvc asp.net-mvc-3

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

EC2上的Windows服务?

你能在ec2上安装和运行自己的Windows服务吗?它有MSMQ吗?

amazon-ec2

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

ExecuteStoreQuery需要什么来尊重关系?

目前,当它失败时,我觉得有点武断.这是我的情况.

实体Foo:

 class Foo {
      int FooID {get;set;
      User Creator {get;set;}
      Bar TheBar {get;set;}
      DateTime CreatedDateTime {get;set;}
 }
Run Code Online (Sandbox Code Playgroud)

实体用户:

 class User {
      int UserID {get;set;}
      ObjectWhatchamacallit Foos {get;set;}
      DateTime LastLogInDateTime {get;set;}
 }
Run Code Online (Sandbox Code Playgroud)

所以

 return DB.ExecuteStoreQuery<Foo>("SELECT *, 
      Created AS CreatedDateTime,
      LastLogIn AS LastLogInDateTime
      FROM
      [User] 
      JOIN Foo ON Foo.CreatorID = [User].UserID
      JOIN Bar ON Foo.BarID = Bar.BarID",
      "Foo");
Run Code Online (Sandbox Code Playgroud)

将加载Foos罚款,与酒吧,但不是造物主.

 return DB.ExecuteStoreQuery<User>("SELECT *, 
      Created AS CreatedDateTime,
      LastLogIn AS LastLogInDateTime
      FROM
      [User] 
      JOIN Foo ON Foo.CreatorID = [User].UserID",
      "User");
Run Code Online (Sandbox Code Playgroud)

没有更好的表现.用户已加载,但没有Foo.

这可能是因为

  1. Foo和User中的别名?
  2. 用户是SQL关键字?(我已经尝试了aliasin …

entity-framework entity-framework-4

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

函数在c#中取任意类型的数组

我已经创建了一个处理对象数组的函数process(Object []).它适用于任何类型,包括整数和浮点数,只要您先将每个元素打包.我希望这个函数能够获取未装箱的元素,如果只是在处理之前自己装箱.我怎么做?

我可以用一些函数来包装,比如process(int [])和process(float []),但这似乎也很麻烦.我已尝试过程(ValueType [])但编译器仍然选择进程(Object []).

我有C#2.0,但如果有一个很好的3.0解决方案我想看到它.

c# arrays types object

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

下划线字符串模型绑定器

我的印象是,当绑定到复杂模型时,处理所有公共属性并尝试对每个属性进行匹配绑定.

我正在尝试解决变量命名问题以便建模

class Model {
      public string Foo {get;set;}
      public string FooBar {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

与查询字符串很好地配合使用

?foo=foo&foo_bar=foo_bar
Run Code Online (Sandbox Code Playgroud)

有没有比自定义模型绑定器更好的方法?无论如何,我的工作不起作用.简单地跳过了FooBar.

public class StringModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var model = base.BindModel(controllerContext, bindingContext);

            if (model != null)
                return model;

            var modelName = Regex.Replace(bindingContext.ModelName, "([a-z])([A-Z])", "$1_$2").ToLowerInvariant();

            var value = bindingContext.ValueProvider.GetValue(modelName);

            return value;
        }

    }
Run Code Online (Sandbox Code Playgroud)

注册

ModelBinders.Binders.Add(typeof(string), new StringModelBinder());
Run Code Online (Sandbox Code Playgroud)

model-binding asp.net-mvc-4

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

特定的通用接口

我正在重构各种类型的所有存储库接口.它们中的大多数都包含非常类似的方法,如Add,Update,但有些方法只对特定类型有意义.这是一个最佳实践问题.

我想过使用泛型来理顺事物.

 public interface IRepository<T>
 {
      T Get(int id);
      void Add(T x);
 }
Run Code Online (Sandbox Code Playgroud)

但现在针对具体方法.我当然可以"继承"接口,但那时我并没有比以前更好.我会有像这样的代码:

 IUserRepository<User> users;
Run Code Online (Sandbox Code Playgroud)

一个巧妙的方法是,如果我可以有多个约束,如:

 public partial interface IRepository<T>
 {
      T Get(int id);
      void Add(T x);
 }

 public partial interface IRepository<T> where T: User
 {
      T Get(Guid id);
 }

 public partial interface IRepository<T> where T: Order
 {
      T Get(string hash);
 }
Run Code Online (Sandbox Code Playgroud)

但编译器抱怨存在冲突的继承.另一种方式是对方法的限制:

 public partial interface IRepository<T>
 {
      T Get(int id);
      void Add(T x);

      T Get(Guid id) where T: User;
      T Get(string hash) where T: Order;
 }
Run Code Online (Sandbox Code Playgroud)

但这并不是这些工作的方式.当然,编译器不是我的意图,而是想要对方法进行类型定义.

现在我只有抛出NotImplemented的方法.丑陋. …

c# generics interface repository-pattern

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

为什么Enumerable.Count()抛出`InvalidOperationException`将`null`转换为`bool`?

任何人都可以解释以下声明如何生成

System.InvalidOperationException:值null不会被赋予System.Boolean类型的成员,因为它是一个不能具有null值的值类型(从瑞典语(另请参见)自由翻译).

if (user.Friends.Count() == 0)
Run Code Online (Sandbox Code Playgroud)

用户是用户,朋友是用户IEnumerable<User>.

更新:朋友从linq返回到sql调用,实际上是一个WhereSelectEnumerableIterator.在这种情况下它是空的,所以我曾期望上面评估为真.如果朋友不是空的,这种方法很好.因此,出于某种原因,当它出现空洞的情况下降时,很高兴知道原因,但我想我真正要问的是,解决方法是什么?

.net c# ienumerable

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

RegisterGlobalFilters和HandleErrorAttribute不起作用

在与RegisterGlobalFilters和HandleErrorAttribute永远斗争之后,我决定回到原点.我使用模板在VS10中创建了一个新的MVC 3项目.在About-action中添加一个抛出DivideByZeroException并启动dev服务器.预计不会看到黄色的屏幕.

但我做到了.

为什么这对我不起作用?

更新

archil和Adam Tuliper的建议有点奏效.调用了Error视图.

然后我继续在RegisterGlobalFilters中添加它.

filters.Add(new HandleErrorAttribute { ExceptionType = typeof(DivideByZeroException), View = "DivideByZeroException", Order = 1 });
filters.Add(new HandleErrorAttribute { View = "AllOtherExceptions", Order = 2 });
Run Code Online (Sandbox Code Playgroud)

调用了AllOtherExceptions视图.为什么不是DivideByZeroException视图?

后续问题已在此处发布.

asp.net-mvc handleerror asp.net-mvc-3

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