今天这真的让我很难过.我确定它不是那么难,但我有一个System.Reflection.PropertyInfo对象.我想根据数据库查找的结果设置其值(想想ORM,将列映射回属性).
我的问题是如果DB返回的值是DBNull,我只想将属性值设置为默认值,与调用相同:
value = default(T); // where T is the type of the property.
Run Code Online (Sandbox Code Playgroud)
但是,如果你给它一个Type,那么default()方法将无法编译,这就是我所拥有的:
object myObj = ???; // doesn't really matter. some arbitrary class.
PropertyInfo myPropInf = ???; // the reflection data for a property on the myObj object.
myPropInf.SetValue(myObj, default(myPropInf.PropertyType), null);
Run Code Online (Sandbox Code Playgroud)
以上不编译.默认(类型)无效.我还想过:
object myObj = ???;
PropertyInfo myPropInf = ???;
myPropInf.SetValue(myObj, Activator.CreateInstance(myPropInf.PropertyType), null);
Run Code Online (Sandbox Code Playgroud)
但是,如果Type是字符串,那将赋值"new String()",但我真的想要"null",这就是"default(string)"将返回的内容.
那我在这里错过了什么?我想一个非常hacky的方法是创建myObj的Type的新实例并复制属性,但这看起来很愚蠢......
object myObj = ???;
PropertyInfo myPropInf = ???;
var blank = Activator.CreateInstance(myObj.GetType());
object defaultValue = myPropInf.GetValue(blank, null);
myPropInf.SetValue(myObj, defaultValue, null);
Run Code Online (Sandbox Code Playgroud)
我宁愿不浪费内存来创建一个全新的实例,只是为了获得属性的默认值.似乎非常浪费. …
每当我尝试在CSS中做一些看似简单的事情时,它都行不通.
我有一个包含460x160图像的内容div.我想要做的就是将图像放在右下角并将文本环绕在它周围.
<div id="contents">
<img src="..." />
text text text text text text ...
</div>
Run Code Online (Sandbox Code Playgroud)
所以我希望它看起来像:
------------------
| text text text |
| text text text |
| text text -----|
| text text | |
------------------
Run Code Online (Sandbox Code Playgroud)
使用左上角或右上角图像进行操作是蛋糕:
#contents img { float:right; }
------------------
| text text | |
| text text -----|
| text text text |
| text text text |
------------------
Run Code Online (Sandbox Code Playgroud)
现在我该怎么把它推到底部?到目前为止,我提出的最好的是:
#contents img { float:right; margin-top: 90%} // really needs to be 100%-160px
------------------
| text …Run Code Online (Sandbox Code Playgroud) 有谁知道Web.Config在不同的构建/部署环境之间管理文件的任何好的工具/实用程序?
例如,我有一个WCF项目,在开发中我不想启用SSL,但我确实希望它在生产中启用.我想要不同的日志记录设置,不同的数据库连接字符串,不同的错误处理,不同的文件路径...甚至一些不同的Unity框架绑定(连接模拟单元测试而不是用于部署的真实对象).
维护单个副本Web.Config是一件痛苦的事,因为添加新的Web服务意味着编辑多个文件并使它们保持同步.
我也注意到,如果你Web.Config手工捣乱太多,如果你尝试使用"添加项目"向导,比如为WCF添加一个新的Web服务,Visual Studio将会窒息,因为它必须修改Web .Config添加端点,nd不能解析它.所以我必须小心不要使现有的Web.Config无效.
我还考虑过使用一些正则表达式进行替换,只是Web.Config在预构建命令中构建一个新的.这似乎是迄今为止最好的选择......
还有其他想法吗?看起来这应该是一个非常普遍的问题,因为Web.Config在开发和生产部署之间可能永远不会是相同的.
更新:
我决定编写一个快速控制台应用程序,它将获取给定目录中的所有xml文件并将它们合并为一个,并且仅包含基于名称的某些文件.
所以我可以在一个目录中:
WebConfig_All
<configuration>
<configSections>
...
</configSections>
<system.web>
...
</system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)
connectionStrings_Debug
<configuration>
<connectionStrings>
<add name="connstr" connectionString="...dev..." />
</connectionStrings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
connectionStrings_Release
<configuration>
<connectionStrings>
<add name="connstr" connectionString="...prod..." />
</connectionStrings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
然后运行我的命令行工具,并传入配置(Debug,Release,custom ...)并将合并所有以_All" or_ <configuration>` 结尾的文件.
所以现在我将80%的Web.Config放在一个WebConfig_All文件中,20%的自定义内容放在每个构建配置的单独文件中.然后,我可以将我的命令行工具作为VisualStudio中的预构建任务运行,或者从NAnt运行,或者在我想要的任何地方运行...
我还使我的XML合并逻辑足以处理以下内容:
<x>
<y a="1">
<z a="1"/>
</y>
</x>
Run Code Online (Sandbox Code Playgroud)
合并
<x>
<y a="1">
<z a="2"/>
</y>
<y a="2"/>
</x>
Run Code Online (Sandbox Code Playgroud)
结果是:
<x>
<y a="1"> …Run Code Online (Sandbox Code Playgroud) 我有一个带out参数的方法,尝试进行类型转换.基本上:
public void GetParameterValue(out object destination)
{
object paramVal = "I want to return this. could be any type, not just string.";
destination = null; // default out param to null
destination = Convert.ChangeType(paramVal, destination.GetType());
}
Run Code Online (Sandbox Code Playgroud)
问题是,通常会有人称之为:
string output;
GetParameterValue(output);
Run Code Online (Sandbox Code Playgroud)
这将失败,因为:
destination.GetType()
Run Code Online (Sandbox Code Playgroud)
destination为null,因此我们无法调用.GetType()它.我们也不能打电话:
typeof(destination)
Run Code Online (Sandbox Code Playgroud)
因为destination是变量名而不是类型名.
那么有没有办法获得设置为null的对象的类型?我认为必须有一种方法可以知道什么类型的存储位置没有分配任何东西.
只是为了提供更多信息,我试图创建一个实用程序方法来获取Oracle存储过程的输出参数.问题是DbParameter.Value对象类型.
对于开发人员来说,最理想的是:
string val = GetParameterValue("parameterName");
Run Code Online (Sandbox Code Playgroud)
值得注意的是,没有类型的铸造.在实践中,你不知道"等于"的lparam,所以我选择了:
string val;
GetParameterValue("parameterName", out val);
Run Code Online (Sandbox Code Playgroud)
在方法中,我会知道输出变量的目标类型.我猜这是一个不好的假设.作为替代方案,我也写了这个方法:
public T GetParameterValue<T>(string paramName)
Run Code Online (Sandbox Code Playgroud)
所以开发人员可以这样做:
string val = GetParameterValue<string>("parameterName");
Run Code Online (Sandbox Code Playgroud)
我发现显式的"字符串"声明是重复的,特别是因为在实践中,目标可能是对象属性和oracle数据类型可能会改变(想想ORM):
MyObj.SomeProp = GetParameterValue<MyObj.SomeProp.GetType()>("parameterName");
Run Code Online (Sandbox Code Playgroud)
但同样,如果MyObj.SomeProp为null,则该.GetType()调用失败.VM必须知道它的类型MyObj.SomeProp …
我不得不放弃基本的WCF UserName/Pwd安全性并实现我自己的自定义客户端凭据,以保存超出默认提供的更多信息.
我参与了这篇MSDN文章,但我遗漏了一些东西,因为它不起作用.
首先,我有一些自定义ClientCredentials,它们提供自定义的ClientCredentialsSecurityTokenManager:
public class CentralAuthCredentials : ClientCredentials
{
public override System.IdentityModel.Selectors.SecurityTokenManager CreateSecurityTokenManager()
{
return new CentralAuthTokenManager(this);
}
}
public class CentralAuthTokenManager : ClientCredentialsSecurityTokenManager
{
private CentralAuthCredentials credentials;
public CentralAuthTokenManager(CentralAuthCredentials creds) : base(creds)
{
this.credentials = creds;
}
public override SecurityTokenProvider CreateSecurityTokenProvider(SecurityTokenRequirement tokenRequirement)
{
if (this.IsIssuedSecurityTokenRequirement(tokenRequirement) || tokenRequirement.TokenType == CentralAuthToken.TOKEN_TYPE)
return new CentralAuthTokenProvider(credentials.UserId, credentials.UserPassword, credentials.ImpersonateId, credentials.LoginType);
else
return base.CreateSecurityTokenProvider(tokenRequirement);
}
public override SecurityTokenAuthenticator CreateSecurityTokenAuthenticator(SecurityTokenRequirement tokenRequirement, out SecurityTokenResolver outOfBandTokenResolver)
{
outOfBandTokenResolver = null;
if (this.IsIssuedSecurityTokenRequirement(tokenRequirement) || tokenRequirement.TokenType == …Run Code Online (Sandbox Code Playgroud) 我正在为开发服务器部署ASP.NET和Web服务解决方案到IIS .看起来执行此作业的最后一个人也部署了所有.pdb文件.我问过它,并被告知如果它们留在服务器上,它们"在日志中提供更好的堆栈跟踪信息".
有没有道理呢?我总是将它们留在后面,从不将它们部署到本地机器以外的任何地方.
对于内部开发IIS服务器(不是生产,外部无法访问)是否有理由部署.pdb文件?有什么不好可能发生吗?他们真的提供任何好处吗?
我有一个类,其中有大约20个方法.每个人都做一些Web服务消息处理.我只需对其进行更改,并意识到这些方法中的每一个都具有完全相同的try/catch:
try
{
/* *** actual processing specific to each method goes here *** */
}
catch (FaultException<CustomException> cfex)
{
// common stuff
}
catch (CustomException cfex)
{
// common stuff
}
catch (Exception ex)
{
// common stuff
}
finally
{
FinalizeServiceCall(wsBus, wsMessage, response, logProps);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是; 而不是在每个方法中都有这个完全相同的try/catch块,有没有办法让它变得常见?我的想法是.NET有这样的东西TransactionScope以某种方式检测离开该块时是否发生异常.有没有我可以利用这样的东西来制作一个常见的try/catch块?还有其他想法吗?
我知道我可以像这样初始化一个JS对象数组:
var things = [
{
prop1: 'foo',
prop2: 'bar'
},
{
prop1: 'foo',
prop2: 'bar'
}
];
Run Code Online (Sandbox Code Playgroud)
我想我会称这些'匿名'类型(抱歉,我使用的是C#/.NET语言).
如果我希望这些原型相同怎么办?所以我定义了一个构造函数:
var Thing = function Thing() {
};
Thing.prototype.prop1 = 'default value';
Thing.prototype.prop2 = 'default value';
Run Code Online (Sandbox Code Playgroud)
现在我想要上面原始代码中的两个项目都是Things.有没有办法做到这一点?
如果我猜,我会说可能是这样的:
var things = [
new Thing() {
prop1: 'foo',
prop2: 'bar'
},
new Thing() {
prop1: 'foo',
prop2: 'bar'
}
];
Run Code Online (Sandbox Code Playgroud)
这基本上是C#对象的初始化语法.我想避免的是:
var thing1 = new Thing();
thing1.prop1 = 'foo';
thing1.prop2 = 'bar';
var thing2 = new Thing();
thing2.prop1 = 'foo'; …Run Code Online (Sandbox Code Playgroud) 我有一个过程,它接受一个表值参数,以及其他:
CREATE PROCEDURE [dbo].[Update_Records]
@currentYear INT,
@country INT,
@records Record_Table_Type READONLY
AS
Run Code Online (Sandbox Code Playgroud)
我试图用Dapper.TVP来称呼它.
这是我到目前为止的代码:
var recordsParameter = new List<SqlDataRecord>();
// This metadata matches 'Record_Table_Type' in the Database
var recordsMetaData = new[]
{
new SqlMetaData("OriginalValue", SqlDbType.Decimal, 19, 4),
new SqlMetaData("NewValue", SqlDbType.Decimal, 19, 4),
new SqlMetaData("NewPercent", SqlDbType.Decimal, 7, 2),
};
foreach (var r in records)
{
var record = new SqlDataRecord(recordsMetaData);
record.SetDecimal(0, r.OriginalValue);
record.SetDecimal(1, r.NewValue);
record.SetDecimal(2, r.NewPercent);
recordsParameter.Add(record);
}
var spParams = new DynamicParameters(new
{
currentYear = filter.currentYear,
country = filter.country,
}); …Run Code Online (Sandbox Code Playgroud) 我有3台服务器,所有服务器都运行Windows Web Server,.NET 4和IIS7.我的web.config文件的开头部分是:
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, …Run Code Online (Sandbox Code Playgroud) .net ×5
c# ×4
asp.net ×3
wcf ×2
web-services ×2
css ×1
css-float ×1
dapper ×1
gettype ×1
html ×1
iis ×1
iis-7 ×1
javascript ×1
reflection ×1
sql-server ×1
token ×1
try-catch ×1
types ×1
web-config ×1