在C#代码中是否存在位置参数不足的情况?我真的没有看到命名参数的任何好处,相反我可以看到过度使用命名参数会使代码难以阅读?所以我的问题是,为什么有人会使用它们,以及它如何帮助编写更好的代码,因为我确信它们没有理由没有实现?
这看起来更干净:
private void Foo(1, true);
Run Code Online (Sandbox Code Playgroud)
然后:
private void Foo(bar: 1, baz: true);
Run Code Online (Sandbox Code Playgroud) 在运行时更改Form语言的正确方法是什么?
InitializeComponent();关于这一点有很多一半的书面线程,但没有一个提供真正的答案,正确的方法是什么?
更新:
澄清我的问题:
做这样的事情:
public Form1()
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("de");
this.InitializeComponent();
}
Run Code Online (Sandbox Code Playgroud)
工作正常,我的所有控件和资源中的其他所有内容都可以正确翻译.做一些像:
private void button1_Click(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
}
Run Code Online (Sandbox Code Playgroud)
什么都不做,表格保持我以前设置的语言 InitializeComponent();
我有一个问题,我签署xml文档并在此之后验证签名并验证通过,但是当我将文档序列化为字节数组然后将其反序列化回文档时,签名验证失败.
以下是用于验证和序列化/反序列化的方法:
public class DocumentSigner {
@Override
public byte[] transformToByteArray(Document doc) throws Exception {
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
ByteArrayOutputStream os = new ByteArrayOutputStream();
transformer.transform(new DOMSource(doc), new StreamResult(os));
return os.toByteArray();
}
private Document byteArrayToXmlDoc(byte[] documentoXml) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new ByteArrayInputStream(documentoXml), "UTF-8");
}
@Override
public Boolean validate(byte[] byteArrayDoc, Integer certificatePropertiesId) throws Exception {
Document doc = byteArrayToXmlDoc(byteArrayDoc);
return validate(doc, certificatePropertiesId);
}
public Boolean validate(Document doc, Integer …Run Code Online (Sandbox Code Playgroud) 我有很多依赖于HttpContext.Current的代码,我注意到来自SignalR集线器的请求有HttpContext.Current == null,所以我的代码断了,例如:
HttpContext.Current.Request.IsAuthenticated
Run Code Online (Sandbox Code Playgroud)
所以我想出了以下内容:
public static class UnifiedHttpContext
{
private static HubCallerContext SignalRContext { get; set; }
private static int SignalRUserId
{
get { return WebSecurity.GetUserId(SignalRContext.User.Identity.Name); }
}
private static bool IsSignalRRequest
{
get { return SignalRContext != null; }
}
public static void SetSignalRContext(HubCallerContext context)
{
SignalRContext = context;
}
public static bool IsAuthenticated
{
get
{
if (!IsSignalRRequest)
{
return System.Web.HttpContext.Current.Request.IsAuthenticated;
}
else
{
return SignalRContext.User.Identity.IsAuthenticated;
}
}
}
public static int UserId
{
get …Run Code Online (Sandbox Code Playgroud) 我有一个与webforms一起使用的现有SQL成员资格数据库,我正在尝试将其设置为使用mvc4但没有运气,当我尝试通过id获取用户时(我知道这个用户存在)我得到null异常.并且web我打开web app配置我可以清楚地看到它没有成员或角色..等等.
以下是来自我的网站应用程序的配置的一部分:
<configuration>
<connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=myserver;Initial Catalog=mydb;User ID=myid;Password=mypwd" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
<membership>
<providers>
<clear />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
<properties>
<add name="UrgentPoints" type="System.Int32" defaultValue="0" />
</properties>
</profile>
<roleManager enabled="true">
<providers>
<clear />
<add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
<add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" …Run Code Online (Sandbox Code Playgroud) 因为我试图用平滑和渲染的每个组合绘制字符串,Graphics.DrawString()我认为文本渲染器会更好地绘制我的字符串,但我认为是错误的.
这是它应该是这样的:

这就是它的样子:

这是我的代码:
Graphics objGraphics2 = Graphics.FromImage(objBitmap);
objGraphics2.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
objGraphics2.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
objGraphics2.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
objGraphics2.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
Font textFont = new Font(textFontFamily, PxtoEm(textSize));
SolidBrush b = new SolidBrush(textColor);
TextRenderer.DrawText(objGraphics2, textValue, textFont, new Rectangle(0, 0, Width, Height), textColor);
Run Code Online (Sandbox Code Playgroud)
我的PxtoEm方法错了吗?
public float PxtoEm(int px)
{
float em = (float)(Convert.ToDouble(Convert.ToDouble(px) * Convert.ToDouble(72) / Convert.ToDouble(objBitmap.HorizontalResolution)));
return em;
}
Run Code Online (Sandbox Code Playgroud)
我需要一些建议,因为这非常糟糕,更大的字体和图像不会缩小,情况会变得更糟.
更新:使用更大的字体(即20px),但使用较小的字体,它会在某些字母上被删除:
这就是它与字体Arial 10px一起使用的方式:

这是结果 Graphics.DrawString()

你可以看到它真的不是很好,但我最接近.我对代码进行了一些更改,并使用更大的字体获得了更好的结果
这就是如何使用字体Arial 20px:

这是绘图结果:

这里是更改的代码(我直接使用em方法并使用像素,切换到Graphics.DrawString()而不是TextRenderer.DrawText()
Graphics objGraphics = Graphics.FromImage(objBitmap);
objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
objGraphics.TextRenderingHint …Run Code Online (Sandbox Code Playgroud) 这工作正常:
self.getById = function(id) {
return ko.utils.arrayFirst(self.PostArray(), function(item) {
if (item.postId === id) {
return item;
}
else {
return 'not found';
}
});
};
console.log(self.PostArray().length);
console.log(self.getById(170));
Run Code Online (Sandbox Code Playgroud)
但是如果我把return ''或者return null在其他块中我总是得到null,那为什么呢?
我有app使用查询字符串来传递页面周围的一些值,我发现了几个关于如何加密查询字符串中的值的例子,但问题是我的KEYS告诉更多关于查询字符串然后将所有整数转换为字符串的值.
有没有办法加密ASP.NET中的整个查询字符串,包括键和键值?
就像是:
Default.aspx?value1=40&value2=30&value3=20
Run Code Online (Sandbox Code Playgroud)
至
Default.aspx?56sdf78fgh90sdf4564k34klog5646l
Run Code Online (Sandbox Code Playgroud)
谢谢!
我尝试在我的SQL服务器上使用分区功能来对我的一个大表进行分区,但是我得到一个错误,说"分区功能只能在SQL Server的企业版中创建.只有SQL Server的企业版支持分区." 所以我想知道没有企业版的其他人如何解决这个问题呢?
任何建议都会很棒!
我怎样才能将param1和param2同时送到我的控制器,我试过这样但只有param1才能通过.
@using (Ajax.BeginForm("Index", "Controller", new { param1 = 0 }, new AjaxOptions { UpdateTargetId = "Target", InsertionMode = InsertionMode.Replace, OnFailure = "error" }))
{
<input type="submit" name="param2" value="1" />
//more buttons
}
public ActionResult Index(int param1, int param2)
{
//do something
}
Run Code Online (Sandbox Code Playgroud) c# ×7
asp.net ×3
asp.net-mvc ×2
certificate ×1
encryption ×1
httpcontext ×1
java ×1
javascript ×1
knockout.js ×1
localization ×1
query-string ×1
signalr ×1
sql ×1
winforms ×1
xml ×1