Tag: reflection

反映程序集会导致Unity需要Microsoft.Practices.ServiceLocation

我们通常只在我们的应用程序中引用Microsoft.Practices.Unity.dll。 我们只使用基本function,这很好用。 在一个应用程序中,使用reflection的行为导致Unity需要另一个DLL。 例如,创建一个控制台应用程序并仅引用Microsoft.Practices.Unity(文件版本2.0.414.0)。 输入以下代码并运行它: class Program { static void Main() { using (var container = new UnityContainer()) { container.RegisterType(); var thing = container.Resolve(); thing.DoSomething(); Console.WriteLine(); LoadSchemaLoaders(); } } public static void LoadSchemaLoaders() { var type = typeof(ISchemaLoader); try { // Get all loaded assemblies, including Unity. // Get all of the types. // Filter […]

是否可以使用reflection从构造函数中获取可选参数值?

鉴于这样的类: public abstract class SomeClass { private string _name; private int _someInt; private int _anotherInt; public SomeClass(string name, int someInt = 10, int anotherInt = 20) { _name = name; _someInt = someInt; _anotherInt = anotherInt; } } 是否可以使用reflection来获取可选参数的默认值?

来自属性的C#自定义属性

所以我有一个我想要循环的类的属性集合。 对于每个属性,我可能有自定义属性,所以我想循环这些属性。 在这种特殊情况下,我在City Class上有一个自定义属性 public class City { [ColumnName(“OtroID”)] public int CityID { get; set; } [Required(ErrorMessage = “Please Specify a City Name”)] public string CityName { get; set; } } 该属性定义如此 [AttributeUsage(AttributeTargets.All)] public class ColumnName : System.Attribute { public readonly string ColumnMapName; public ColumnName(string _ColumnName) { this.ColumnMapName= _ColumnName; } } 当我尝试遍历属性[工作正常]然后遍历属性时,它只是忽略属性的for循环并且不返回任何内容。 foreach (PropertyInfo Property […]

从Attribute转到CustomAttributeData或向后

题。 有没有办法根据我的自定义属性的给定实例获取CustomAttributeData的实例,比如, MyAttribute ? 或相反亦然? 我为什么需要这个? MyAttribute的实例包含我感兴趣的属性,而CustomAttributeData的实例包含我感兴趣的实际构造函数参数。所以现在我实现了双重工作: 首先 ,通过调用获取MyAttribute的实例 Attribute.GetCustomAttribute(property, typeof(MyAttribute)) as MyAttribute , 第二 ,通过调用获取CustomAttributeData的实例 CustomAttributeData.GetCustomAttributes(property) 并走过这个系列。 PS我已经看过这个问题 ,但没有在那里找到理想的解决方案。

如何将DbSet 转换为List

鉴于以下简化的Entity Framework 6上下文,我尝试使用实体填充List,但是如何通过reflection进行转换(我相信)有问题。 public class FooContext : DbContext { public virtual IDbSet Foo { get; set; } //… } public class FooClass { public int Id{ get; set; } public string Name {get; set; } //… } public main() { using (var context = new FooContext()) { var sets = typeof(FooContext).GetProperties().Where(pi => pi.PropertyType.IsInterface && pi.PropertyType.GetGenericTypeDefinition().ToString().ToLower().Contains(“idbset”)); […]

在c#中用reflection或类似的(没有第三方库)替换Property Getter / Setter

例如,我有一个包含在类中的属性 public class Greeter { private Hashtable _data; public string HelloPhrase { get; set; } public Greeter(data) { _data = data; } } 我想要做的是向HelloPhrase属性添加一个属性,就像这样 [MyCustomAttribute(“Hello_Phrase”)] public string SayHello { get; set; } 这样在构造函数中我可以反映已定义MyCustomAttribute的类(Greeter)中的Properties,并将属性的Get / Set方法设置为匿名方法/委托。 public Greeter(data) { _data = data; ConfigureProperties(); } 我已经设法从类中获取PropertyInfo,但这只暴露了GetSetMethod(http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.getsetmethod.aspx)和相应的GetGetMethod。 我已经在这里和网上阅读了一些问题,但找不到不使用某种Aspects库的答案。 任何人都可以提供指向在运行时设置Get / Set方法的指针吗? 理想情况下,像一个代表 x =>_data[keyDefinedByAttribute];

如何获取具有其姓名字符串的类成员值?

可能重复: 如何动态读取C#类的属性? 我必须使用其名称的字符串来获取类成员的值。 我想我必须使用reflection,但我不确定如何。 你能帮助我吗?

如何反复告知属性是否具有公共Setter

我将对象模型保存到XML但是当我将其加载回来时,我在尝试使用PropertyInfo.SetValue()时会遇到exception,因为该属性没有setter只是一个getter。 我想要么不保存只有getter的属性或者在加载时弄清楚它是否适合我尝试设置值。 任何人都知道如何做到这一点 干杯

使用reflection创建通用IList实例

我正在尝试使用reflection创建一个通用的对象列表。 以下代码抛出错误无法创建接口的实例。 。 我可以将IList更改为List并且它工作正常,但我想知道是否有办法让这个与IList一起工作。 var name = typeof (IList).AssemblyQualifiedName; Type type = Type.GetType(name); var list = Activator.CreateInstance(type);

如何检索generics方法的名称,包括generics类型名称

在C# ,我有一个带有以下签名的方法: List Load(Repository repository) 在Load()方法中,我想转储完整的方法名称(用于调试目的),包括generics类型。 例如:调用Load(); 会写”Load” 到目前为止我尝试了:使用MethodBase.GetCurrentMethod()和GetGenericArguments()来检索信息。 List Load(Repository repository) { Debug.WriteLine(GetMethodName(MethodBase.GetCurrentMethod())); } string GetMethodName(MethodBase method) { Type[] arguments = method.GetGenericArguments(); if (arguments.Length > 0) return string.Format(“{0}”, method.Name, string.Join(“, “, arguments.Select(x => x.Name))); else return method.Name; } 检索方法名称有效,但对于通用参数,它总是返回”T” 。 方法返回Load而不是Load (这是无用的) 我试图在GetMethodName() GetGenericArguments()之外调用GetGenericArguments()并将其作为参数提供,但它没有帮助。 我可以提供typeof(T)作为GetMethodName()的参数(它将起作用)但是它将特定于generics类型的数量,例如:使用Load它将不再起作用,除非我提供另一个论点。