- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想用 FastMember ( https://www.nuget.org/packages/FastMember/ ) 替换我的反射索引访问器,但偶然发现了以下问题。
我有以下设置:
class Program
{
static void Main(string[] args)
{
var d = new DerivedClass
{
Name = "Derived Name Property",
BaseName = "Base Name Property",
BaseInternalName = "Base Internal Name Property",
};
Console.WriteLine(d["Name"]);
Console.WriteLine(d["BaseName"]);
Console.WriteLine(d["BaseInternalName"]);
Console.ReadLine();
}
}
public abstract class BaseClass
{
public object this[string propertyName]
{
get
{
var x = GetTypeAccessor();
return x[this, propertyName];
}
set
{
var x = GetTypeAccessor();
x[this, propertyName] = value;
}
}
protected abstract TypeAccessor GetTypeAccessor();
public string BaseName { get; set; }
internal string BaseInternalName { get; set; }
}
public class DerivedClass : BaseClass
{
public string Name { get; set; }
private TypeAccessor _typeAccessor;
protected override TypeAccessor GetTypeAccessor()
{
if (_typeAccessor == null)
_typeAccessor = TypeAccessor.Create(this.GetType(), true);
return _typeAccessor;
}
}
有了这个,我在行 Console.WriteLine(d["BaseInternalName"]);
“System.ArgumentOutOfRangeException”类型的未处理异常发生在 FastMember_dynamic 中
Innerexception 为空。
根据 nuget https://www.nuget.org/packages/FastMember/从 1.0.0.8 版本开始,应该支持访问非公共(public)属性:
我注意到的另一件事是,在 nuget 中它说 1.0.0.11 是最新版本,但是通过 Install-Package FastMember
下载到我的计算机的 dll 版本为 1.0.0.9 , 也许马克 https://stackoverflow.com/users/23354/marc-gravell看到这个并可以修复它。 :)
最佳答案
深入研究 TypeAccessor
中的代码(或者更准确地说是派生的 DelegateAccessor
),您可以看到使用了 allowNonPublicAccessors
作为获取非公共(public)属性 getter/setter 的值,而不是非公共(public)属性/字段。
这是相关的代码片段(在 TypeAccessor.WriteMapImpl
内):
PropertyInfo prop = (PropertyInfo)member;
MethodInfo accessor;
if (prop.CanRead && (accessor = (isGet ? prop.GetGetMethod(allowNonPublicAccessors) : prop.GetSetMethod(allowNonPublicAccessors))) != null)
此外,您还可以看到 CreateNew
仅尝试访问公共(public)实例字段/属性:
PropertyInfo[] props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
因此,您不能通过 TypeAccessor
对象访问任何非公共(public)字段/属性。
关于c# - Fastmember 访问非公共(public)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26803788/
当使用 TypeAccessor.Create FastMember 时,似乎总是返回按字母排序的列列表。是否可以告诉它保留类中列的顺序? 例如: var testClass = new { B =
我需要提取所有不可枚举的公共(public)读写属性,除非它们是字符串。目前这是通过反射完成的,想知道这是否可以用 FastMember 完成. 我尝试了类似下面的代码,但它没有达到我想要的效果。我可
(这是通过 Twitter 上的一个问题,经许可在此处重新提问) 我正在尝试快速验证某些对象(以测试空值),我认为 FastMember 可能会有所帮助 - 但是,通过下面显示的测试,我发现性能要差得
当我尝试使用 FastMember 设置嵌套成员属性时出现异常。例如当有这些类(class)时 public class A { public B First { get; set; } }
我有以下对象: dynamic person = new {Id = 1, Name = "SpiderMan"}; 我需要能够遍历属性名称,例如“Id”,“Name”。 我还需要能够以最有效的方式实
我想用 FastMember ( https://www.nuget.org/packages/FastMember/ ) 替换我的反射索引访问器,但偶然发现了以下问题。 我有以下设置: class
我从一个我无法控制的库返回一个接口(interface): public interface IA : IB { String A { get;} } public interface IB { St
我不确定它是否受支持,但我试图让 fastmember 为我提供动态 ExpandoObject 的类型信息(使用下面的代码),但我收到错误: System.NotSupportedException
我有一个 C# .NET Core 2.0 Web API 项目(作为微服务实现)。 我在我的存储库层中使用 NuGet 包“Dapper”和“FastMember”(最新版本)来尝试转换 IEnum
我正在使用 FastMember转换 List到 Datatable .一些类包含枚举,这在将数据表作为 TVP 传递给存储过程时会导致问题。 public class MyObject {
我编写了一个方法,使用 FastMember 将通用列表转换为数据表来自 NuGet。 这是我的代码: public DataTable ConvertGenericListToDataTable(
我正在使用 FastMember 库将对象列表转换为数据表,但它返回空对象,所以任何人都可以帮我解决这个问题 List list = new List() { new { Number = 500 }
我已经使用这个函数成功地为属性和嵌套属性赋值 private static void AssignValueToProperty(ObjectAccessor accessor, object val
我从 http://code.google.com/p/fast-member/source/checkout 加载了 .net 3.5 FastMember在 VS 2010 中。 编译器生成此错误
我一直遇到这个错误 指定的参数超出了有效值的范围。参数名称:名称 当我几乎只是在这里复制示例时 https://code.google.com/p/fast-member/ 错误发生在 bcp.Wri
我是一名优秀的程序员,十分优秀!