小编Sra*_*van的帖子

如何使用C#Reflection将Vaues设置为嵌套属性.?

我试图使用反射动态地将值设置为类的嵌套属性.任何人都可以帮我这样做.

我正在上课Region.

public class Region
{
    public int id;
    public string name;
    public Country CountryInfo;
}

public class Country
{
    public int id;
    public string name;
}
Run Code Online (Sandbox Code Playgroud)

我有一个Oracle数据阅读器来提供Ref游标中的值.

这会给我一个

ID,姓名,COUNTRY_ID,COUNTRY_NAME

我可以通过下面的方式将值分配给Region.Id,Region.Name.

FieldName="id"
prop = objItem.GetType().GetProperty(FieldName, BindingFlags.Public | BindingFlags.Instance);
prop.SetValue(objItem, Utility.ToLong(reader_new[ResultName]), null);
Run Code Online (Sandbox Code Playgroud)

对于嵌套属性,我可以通过读取Fieldname创建实例来为下面的值赋值.

FieldName="CountryInfo.id"

if (FieldName.Contains('.'))
{
    Object NestedObject = objItem.GetType().GetProperty(Utility.Trim(FieldName.Split('.')[0]), BindingFlags.Public | BindingFlags.Instance);

    //getting the Type of NestedObject
    Type NestedObjectType = NestedObject.GetType();

    //Creating Instance
    Object Nested = Activator.CreateInstance(typeNew);

    //Getting the nested Property
    PropertyInfo nestedpropinfo = objItem.GetType().GetProperty(Utility.Trim(FieldName.Split('.')[0]), BindingFlags.Public | …
Run Code Online (Sandbox Code Playgroud)

c# reflection propertyinfo

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

'Model'与声明'System.Web.Mvc.WebViewPage <TModel> .Model冲突

我有一个显示以下客户对象的视图.

public Class Customer
{
    public long Id { get; set; }
    public string Name { get; set; }
    public Address AddressInfo { get; set; }
}

public class Address 
{
    public string Details { get; set; }
    public City CityInfo { get; set; }
    public Region RegionInfo { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

并有一个控制器将客户返回到View

public ActionResult GetCustomer(long Id)
{
    return View("Customer",GetCustomer(Id));
}
Run Code Online (Sandbox Code Playgroud)

最后是View Is,

[Customer.cshtml]
@model Customer;
Name: @Model.Name
Address Details: @Html.Partial("Address",Model)

[Address.cshtml]
@model Customer;
@Model.CityInfo.Name, @Model.RegionInfo.Name
Run Code Online (Sandbox Code Playgroud)

一切似乎都很好.但我得到"'模型'与@ Html.Partial("地址",模型)上的声明'System.Web.Mvc.WebViewPage.Model'错误冲突我之前在许多项目中都做过同样的事情并没有得到问题.

我不知道进一步的进展. …

c# asp.net-mvc razor

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

如何在抽象类的静态属性中使用HttpContext.Current.Session.

我有一个ControllerBase抽象类,如下所示.

using System.Web;
using System.Web.Mvc;
public abstract class ControllerBase : Controller
{
    public static string SesssionId
    {
         get { return HttpContext.Current.Session["SessionId"]; }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到了错误

"对象引用是非静态字段,方法或属性'System.Web.Mvc.Controller.HttpContext.get"所必需的

但是我在其他静态类中使用了相同的并且没有出现上述错误.

我想知道HttpContext是如何可访问的而不是当前的.

谁能澄清我,上面有什么问题.

c# asp.net-mvc

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

如何使用jquery获取表行的属性值?

我有一个以下剃刀来创建一个表格。

@model IEnumerable<iBoxV5.Model.Common.AdvancedSearch>
<table id="SearchFieldsTable">
  <thead>
    <tr>
    <th>
        ColumnName
    </th>
    <th>
        FilterValues
    </th>
    <th>Updated Before</th>
    </tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr ColumnName=@item.ColumnName DatabaseColumn=@item.DatabaseColumn DisplayName=@item.DisplayName Datatype=@item.Datatype id=@(index++)>
    <td>
        @Html.DisplayFor(modelItem => item.ColumnName)
    </td>
    <td>
        @Html.TextBoxFor(modelItem => item.FilterValue, new { @class = "FrmTextBox advSearch" });

        @Html.TextBoxFor(modelItem => item.FilterValue, new { @class = "FrmTextBox advSearch" });
    </td>
    <td>
        @Html.TextBoxFor(modelItem => item.FilterValueTo, new { @class = "FrmTextBox advSearch" });
    </td>
</tr>
}
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

在上面,我为 ColumnName 之类的添加了一些属性。

我想为表的每一行构建一个 JSON。

我尝试使用以下 jquery …

jquery attributes

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

正则表达式从电子邮件中删除不需要的字

我使用以下代码从电子邮件中删除一些不需要的字符.但正则表达式取代了'.' 符号,即使我没有在字符集中提到要删除.

string emailText = @"[\s;'\(\)\[\]!#\$%&\*\+-\?>=<_:\/\""]";
var stringInput = Console.ReadLine(); //Input "sara@gmail.com"
var stringTest = Regex.Replace(stringInput, emailText,string.Empty); //Output "sara@gmailcom"
Run Code Online (Sandbox Code Playgroud)

请帮我解决这个问题.

谢谢和问候,Saravanakumar R.

c# regex

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

标签 统计

c# ×4

asp.net-mvc ×2

attributes ×1

jquery ×1

propertyinfo ×1

razor ×1

reflection ×1

regex ×1