我有一个字符串列表,可以包含一个字母或一个int的字符串表示(最多2位数).它们需要按字母顺序排序,或者(当它实际上是一个int时)对它所代表的数值进行排序.
例:
IList<string> input = new List<string>()
{"a", 1.ToString(), 2.ToString(), "b", 10.ToString()};
input.OrderBy(s=>s)
// 1
// 10
// 2
// a
// b
Run Code Online (Sandbox Code Playgroud)
我想要的是
// 1
// 2
// 10
// a
// b
Run Code Online (Sandbox Code Playgroud)
我有一些想法涉及通过尝试解析它来格式化它,然后如果它是一个成功的tryparse用我自己的自定义stringformatter格式化它使它有前面的零.我希望能有更简单,更高效的东西.
编辑
我最终制作了一个IComparer,我将其转储到我的Utils库中供以后使用.
当我在它的时候,我也在混合物中投掷了双打.
public class MixedNumbersAndStringsComparer : IComparer<string> {
public int Compare(string x, string y) {
double xVal, yVal;
if(double.TryParse(x, out xVal) && double.TryParse(y, out yVal))
return xVal.CompareTo(yVal);
else
return string.Compare(x, y);
}
}
//Tested on int vs int, double vs double, int vs double, …Run Code Online (Sandbox Code Playgroud) 在我的asp.net mvc应用程序中,我使用Ninject作为DI框架.
我的控制器使用我的HttpAccountService从cookie获取信息.为此,我需要HttpAccountService中的HttpContext.Current.由于这是一个依赖项,我通过构造函数注入它:
kernel.Bind<IAccountService>()
.To<HttpAccountService>()
.InRequestScope()
.WithConstructorArgument("context", HttpContext.Current);
Run Code Online (Sandbox Code Playgroud)
遗憾的是,这总是绑定到相同的上下文,这使得在第一个请求完成后,这个上下文变得过时了.
我应该如何正确地注入我的HttpContext?
callstack显示以下内容:
[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache) +86
System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache) +230
System.Activator.CreateInstance(Type type, Boolean nonPublic) +67
System.Activator.CreateInstance(Type type) +6
System.Web.Mvc.DefaultModelBinder.CreateModel(ModelBindingContext bindingContext, Type modelType) +277
System.Web.Mvc.<>c__DisplayClass1.<BindModel>b__0() +98
System.Web.Mvc.ModelBindingContext.get_Model() +51
System.Web.Mvc.DefaultModelBinder.BindModelCore(ModelBindingContext bindingContext) +2600
System.Web.Mvc.DefaultModelBinder.BindModel(ModelBindingContext bindingContext) +1067
System.Web.Mvc.DefaultModelBinder.BindProperty(ModelBindingContext parentContext, Type propertyType, Func`1 propertyValueProvider, String propertyName) +208
System.Web.Mvc.DefaultModelBinder.BindModelCore(ModelBindingContext bindingContext) +1787
System.Web.Mvc.DefaultModelBinder.BindModel(ModelBindingContext bindingContext) +1067
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ParameterInfo parameterInfo) +355
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(MethodInfo methodInfo) +439
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, …Run Code Online (Sandbox Code Playgroud) 今天我注意到,在我ConfigurationManager.ConnectionStrings的第一个实例中.\SQLEXPRESS.我记得明确地从我的web.config中删除了这个条目所以我再次检查,但没有找到任何东西.然后我搜索了整个解决方案,而不是单个匹配.
这个连接字符串来自何处以及如何删除它?
首先,我知道我的标题可以更好地表达,但我的数学课程已经不复存在了,我不记得正确的单词了.
我需要做这样的事情(伪c#)
int[] digits1 = new int[10]{0,1,2,3,4,5,6,7,8,9};
int[] digits2 = new int[10]{0,1,2,3,4,5,6,7,8,9};
int result = digits1*digits2
Run Code Online (Sandbox Code Playgroud)
这将是每个数组的元素[i]的乘积之和.
这显然不起作用.对任何更好的头衔或解决方案的任何建议?
编辑 澄清:我知道我可以循环它们并进行数学计算.基本上我认为有一个更好的方法来做到这一点,我纯粹出于个人的好奇而寻找它.
如何检查我所在的线程是否是Unity线程?
我尝试在构造函数时捕获threadId,但是在程序生命周期的某个地方,threadId会移动.
在我的项目中,一些辅助线程进程需要访问新创建的对象.
我使用生产者 - 消费者模式,因此可以在Unity线程上创建它们.对象工厂将请求排队,并在Update()我请求的对象上在正确的线程上实例化.在Queued和Instantiated之间,工厂方法使用AutoResetEvent等待ObjectCreated事件.
现在有时这个工厂将从主线程调用,AutoResetEvent将阻塞自己的线程.我也尝试过肮脏的方式
// First try on this thread
try
{
return action();
}
catch (ArgumentException ex)
{
Debug.Log("Tried on same thread, but failed. "+ex.Message);
}
PushToQueueAndWait(action);
Run Code Online (Sandbox Code Playgroud)
但是当团结抛出异常时,无论是否捕获,程序都会停止.
如果我可以检查我是否在正确的线程上,我可以在排队和执行之间切换.
我有一个表单,根据网站的品牌,在一个给定的位置应该可以看到两个输入字段中的一个.
我想我只是将两个输入字段放在同一个容器中,然后通过我的样式表设置其中一个显示:none; 这确实隐藏了这个领域,但它仍然占据了空间.我也尝试将高度和宽度设置为0或将可见性设置为隐藏或折叠,但没有一个工作.
直到现在所有的品牌推广都可以用css样式表完成,所以我想保持这种方式.至少应该在IE6及更高版本,Firefox 2及更高版本和Chrome(最新版)中支持该解决方案.
有没有办法将int转换为位掩码?
例:
int i = 33;
Run Code Online (Sandbox Code Playgroud)
应转换为(不确定数据类型)
bool[] bitmask = new[] {true, false, false, false, false, true};
Run Code Online (Sandbox Code Playgroud)
更新
对大多数答案的反应:
我需要这样做:
BitArray bits = new BitArray(BitConverter.GetBytes(showGroup.Value));
List<String> showStrings = new List<string>();
for (int i = 0; i < bits.Length; i++)
{
if(bits[i])
showStrings.Add((i+1).ToString().PadLeft(2, '0'));
}
Run Code Online (Sandbox Code Playgroud)
如果不将其转换为比特阵列,情况会怎样?
如何获取实例成员的值?
对于propertyInfos,有一个propertyInfo.GetValue(instance, index),但memberInfo中不存在这样的东西.
我在网上搜索,但似乎停止获取会员的姓名和类型.
我们想要创建一个公司nuget包存储库.有没有办法通过命令行添加包源,以便我们可以通过设置或其他东西配置我们的新包源?
我们基本上不想去
工具 - 选项 - 包管理器 - 包源 - 加号按钮 - 添加名称和来源
在公司的每台开发者机器上.
c# ×7
.net ×2
asp.net-mvc ×2
arrays ×1
css ×1
formatting ×1
hide ×1
html ×1
httpcontext ×1
math ×1
ninject ×1
nuget ×1
reflection ×1
sorting ×1
tostring ×1