system.reflection

How to get constructor as MethodInfo using Reflection

送分小仙女□ 提交于 2020-01-02 00:50:10
问题 The constructor looks like this: public NameAndValue(string name, string value) I need to get it as a MethodInfo using Reflection. It tried the following, but it does not find the constructor ( GetMethod returns null ). MethodInfo constructor = typeof(NameAndValue).GetMethod(".ctor", new[] { typeof(string), typeof(string) }); What am I doing wrong? 回答1: Type.GetConstructor. Note this returns a ConstructorInfo rather than a MethodInfo, but they both derive from MethodBase so have mostly the

Get object by reflection

倾然丶 夕夏残阳落幕 提交于 2020-01-01 12:31:19
问题 I'm looking for mechanism in c# works like that: Car car1; Car car2; Car car = (Car)SomeMechanism.Get("car1"); car1 and car2 are fields So I want to get some object with reflection, not type :/ How can I do it in c# ? 回答1: It's not possible for local variables but If you have a field, you can do class Foo{ public Car car1; public Car car2; } you can do object fooInstance = ...; Car car1 = (Car)fooInstance.GetType().GetField("car1").GetValue(fooInstance); 回答2: It looks like you're trying to

Include all navigation properties using Reflection in generic repository using EF Core

大城市里の小女人 提交于 2020-01-01 04:55:13
问题 I'm working on creating a generic repository for an EF Core project to avoid having to write CRUD for all models. A major roadblock I've hit is navigation properties not being loaded since Core doesn't yet support lazy loading and the generic class obviously can't define .Include statements for class specific properties. I'm trying to do something like this for my Get method to include all the properties dynamically: public virtual T Get(Guid itemId, bool eager = false) { IQueryable<T>

Explicitly call static constructor

我们两清 提交于 2019-12-31 17:49:13
问题 I want to write unit test for below class. If name is other than "MyEntity" then mgr should be blank. Negative Unit test Using Manager private accessor I want to change name to "Test" so that mgr should be null. And then will verify the mgr value. To achieve this, I want to explicitly call the static constructor but when I call the static constructor using Manager_Accessor.name = "Test" typeof(Manager).TypeInitializer.Invoke(null, null); name is always set to "MyEntity" how to set name to

Best way to get a Type object from a string in .NET

馋奶兔 提交于 2019-12-30 08:56:18
问题 What is the best way to convert a string into a Type object in .NET? Issues to consider: The type may be in a different assembly. The type's assembly may not be loaded yet. This is my attempt, but it doesn't address the second issue Public Function FindType(ByVal name As String) As Type Dim base As Type base = Reflection.Assembly.GetEntryAssembly.GetType(name, False, True) If base IsNot Nothing Then Return base base = Reflection.Assembly.GetExecutingAssembly.GetType(name, False, True) If base

How do I look up the internal properties of a C# class? protected? protected internal?

拟墨画扇 提交于 2019-12-30 04:17:05
问题 If I have a C# class MyClass as below: using System.Diagnostics; namespace ConsoleApplication1 { class MyClass { public int pPublic {get;set;} private int pPrivate {get;set;} internal int pInternal {get;set;} } class Program { static void Main(string[] args) { Debug.Assert(typeof(MyClass).GetProperties( System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Length == 1); Debug.Assert(typeof(MyClass).GetProperties( System.Reflection.BindingFlags.NonPublic | System

I need an alternative to `Assembly.GetEntryAssembly()` that never returns null

感情迁移 提交于 2019-12-28 16:32:10
问题 I need to find the assembly in which managed code execution started. // using System.Reflection; Assembly entryAssembly = Assembly.GetEntryAssembly(); This seems like the way to go, but the MSDN reference page for Assembly.GetEntryAssembly states that this method "[c]an return null when called from unmanaged code." In that case, I would like to know which assembly was called by unmanaged code. Is there a reliable way of doing this, i.e. one that always returns a non-null Assembly reference?

I would like to clear my querystring after using it on page load c#

*爱你&永不变心* 提交于 2019-12-25 09:00:04
问题 Before I ask this question, I'm not even sure what I'm using is a query string (I'm so clueless on this, what I have is the result of some other confusing StackOverFlow research). It is a parameter I'm passing from my SSRS report viewer to my app via a hyperlink expression. It works and everything is grand except for I'd like to clear it from the url right afterwards. http://10.155.54.101/Update?CurrencyId=67 And I am getting the parameter with this logic on page load. if (Request.Params[

Get List of Controls on each WebForm in an Assembly

冷暖自知 提交于 2019-12-25 02:22:51
问题 Is it possible to get a plain text list of controls that are present on a webform using reflection? Basically a colleague is looking to get a list of controls to help define a validation strategy e.g. in general product numbers must be numeric but on certain screens they can be alphanumeric. I thought it would be straightforward using reflection to generate a list of something like: AddProduct.aspx txtProductNumber txtProductName etc. I can get the form names but so far not the controls

Walk from Attribute to CustomAttributeData or backwards

爷,独闯天下 提交于 2019-12-23 09:25:09
问题 Question. Is there a way to get an instance of CustomAttributeData based on the given instance of my custom attribute, say, MyAttribute ? Or vice versa? Why do I need this? The instance of MyAttribute contains properties I am interested in, while the instance of CustomAttributeData contains actual constructor parameters I am interested in. So now I implement double work: first , get the instance of MyAttribute by calling Attribute.GetCustomAttribute(property, typeof(MyAttribute)) as