这是我的第一篇文章,尽管我在某种程度上搜索了与我的问题相关的主题,但我在找到正确的答案时遇到了很多麻烦.
我的问题可能很简单,但我知道答案可能不那么容易.如果存在的话.
话虽如此,这是我的情况:作为一个例子,我有一个PropertyInfo对象数组,我用它来从类中获取属性,如下所示:
public PropertyInfo[] GetProperties (object o)
{
PropertyInfo[] properties = o.GetType().GetProperties();
return properties;
}
Run Code Online (Sandbox Code Playgroud)
看起来很简单吧?现在我的问题是:如何创建一个新的PropertyInfo对象并将其添加到数组?
我看过其他帖子,用户想要设置PropertyInfo的VALUE,但这不是我需要的.我需要的是动态创建一个新的PropertyInfo对象,其中我唯一可用的数据是Name和Type.
我之前发布的测试用例只是我想要实现的一个小例子.事实上,我真正的最终目标是能够基于这个类创建一个新的PropertyInfo:
public class DynamicClass
{
public Type ObjectType { get; set; }
public List<string> PropertyNameList { get; set; }
public List<Type> PropertyTypeList { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我希望有人可以帮助我实现这一目标.提前谢谢了!
编辑:我忘了在GetProperties()方法之前添加o.GetType().谢谢Ilya Ivanov!
我正在调用SelectProperties方法,如下所示:
list = _queriable.Select(SelectProperties).ToList();
Run Code Online (Sandbox Code Playgroud)
该方法如下所示:
private Expression<Func<T, List<string>>> SelectProperties
{
get
{
return value => _properties.Select
(
prop => (prop.GetValue(value, new object[0]) ?? string.Empty).ToString()
).ToList();
} …Run Code Online (Sandbox Code Playgroud) 我在这里关于将我的CSS和脚本捆绑到我的Web窗体应用程序.
首先,我想指出我正在关注本教程:http://blogs.msdn.com/b/rickandy/archive/2012/08/14/adding-bundling-and-minification-to-网络forms.aspx
我已经在App_Start类BundleConfig中看起来像这样:
using System.Web;
using System.Web.Optimization;
namespace SitePessoal.Web
{
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery/core/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
"~/Scripts/jquery/core/jquery-ui-{version}.js"));
bundles.Add(new StyleBundle("~/CSS/css").Include(
"~/CSS/Estilos.css",
"~/CSS/Print.css",
"~/CSS/Styles.css"));
}
}
}
Run Code Online (Sandbox Code Playgroud)
另外,我已经使用Nugget下载了Optimization包,之后我转到了我的Global.asax文件并试图在Application_Start方法中注册它,如下所示:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
BundleConfig.RegisterBundles(BundleTable.Bundles);
BundleTable.EnableOptimizations = true;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这是事情不起作用的地方.不幸的是,它一直用红色强调课程,并给我以下消息:
Error 3 The name 'BundleTable' does not exist in the current context
Error 4 The name 'BundleTable' does not exist in the current …Run Code Online (Sandbox Code Playgroud) 我已经看过很多关于这方面的话题,但不幸的是我相信每个案例都是一个不同的案例(或大多数案例),我真的很想得到一些专家对我的案例的意见,因为我不能让我的代码工作甚至阅读完一些其他主题后.
情况:我在jQuery中使用Ajax请求调用我在WebApi项目中创建的WebService方法以及MVC 4应用程序.
我的WebService控制器类看起来像默认类,如下所示:
public class AdditionalInfoController : ApiController
{
//GET api/AdditionalInfo
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
//GET api/AdditionalInfo/5
public string Get(int id)
{
return "value";
}
//PUT api/AdditionalInfo/5
public void Put(int id)
{
string test = "";
}
}
Run Code Online (Sandbox Code Playgroud)
来自jQuery的我的Ajax请求如下所示:
function GetAdditionalInfo(obj)
{
var request = jQuery.ajax({
url: "/api/AdditionalInfo/Get",
type: "GET",
data: { id: obj.id },
datatype: "json",
async: false,
beforeSend: function () {
},
complete: function () {
}
})
.done(function …Run Code Online (Sandbox Code Playgroud) 我在论坛上搜索过这样的解决方案,但是我找不到与我的特定问题相符的问题.
为了找到问题,这可能需要更有经验的眼睛,所以我感谢所有的帮助!
问题:我正在尝试使用DateTime变量解析带有日期的字符串.但是,即使字符串日期格式完全相同,它仍会引发异常.
我想知道为什么,以及如何解决它.我真的看不出有什么问题!
try
{
string value = "Sep-17-2012 03:04:07 am";
string format = "M-dd-yyyy hh:mm:ss tt";
DateTime temp = DateTime.ParseExact(value, format, CultureInfo.InvariantCulture);
}
catch(Exception e){}
Run Code Online (Sandbox Code Playgroud)
提前致谢,
狂
c# ×3
bundle ×1
datetime ×1
exception ×1
jquery ×1
object ×1
propertyinfo ×1
reflection ×1
web-services ×1
webforms ×1