1,153 questions
0
votes
1
answer
89
views
Deducing instance to handle an object in C#
I am extending my C# J2K library to dynamically register codecs from other assemblies. That part is working, but I am having trouble understanding IsAssignableFrom(TypeInfo) and how I can deduce which ...
0
votes
2
answers
73
views
Is it possible to replicate ASP.NET RouteAttribute functionality in a non-WebAPI project?
I'm currently working on a console application that essentially mimics an ASP.NET Web API.
Everything was going smoothly until I ran into an issue regarding route attributes.
What I'm trying to ...
0
votes
1
answer
98
views
C# Blazor .NET 9: automatically collect all application Policies
I use .NET 9 and blazor, ASP.NET Core Identity - role, policy, claim.
To differentiate access on pages and components, the code is using policies - like this:
@attribute [Authorize(Policy("...
-1
votes
1
answer
70
views
Hash keys match, but Distinct() doesn't remove duplicates with my custom MemberInfo type class only when the MemberType is set to Custom
I got tired of dealing with properties and fields separately so I made a VariableInfo strategy pattern to handle both without having to know what type it is. You feed the context a FieldInfo or a ...
2
votes
1
answer
265
views
Attribute.IsDefined() isn't working for my custom MemberInfo type
The short of it is that I got tired of dealing with properties and fields separately. I made a VariableInfo strategy pattern to handle both without having to know what type it is. You feed the context ...
2
votes
0
answers
47
views
Is there a way to programmatically determine if a compiled cmdlet requires elevation (other than simply running the command)?
Is there a way to use reflection to programmatically inspect and determine if a given compiled cmdlet requires elevation?
Notes:
This question concerns compiled cmdlets, not script (text) files.
...
0
votes
1
answer
86
views
Moq fully generic Setup with class proxy attempt throws `Late bound operations cannot be performed`
PROBLEM TO SOLVE
I'm trying to create a mock of interface ISomeService using Moq library which will wrap its real implementation SomeService.
Right now I'm doing this manually like this:
var instance =...
3
votes
1
answer
126
views
Is there a way of constructing function pointer types using Reflection?
I need some way of constructing a function pointer type that points to a specific function signature. So something like this:
Type[] args = [typeof(int), typeof(int)];
// Pointer to (int -> int) ...
-1
votes
2
answers
135
views
Is it possible to use reflection to compare two arrays with the same element types?
My class looks something like this:
public class Test
{
public A[] ArrayA { get;set; }
}
public class A
{
public string P1 { get;set; }
public int P2 { get;set; }
}
Suppose I have two ...
2
votes
2
answers
186
views
How to Call a C# Method from C++ with Parameters?
using System;
using System.Runtime.InteropServices;
using System.Reflection;
public struct StructCreatedByUser
{
public int x;
public float anything;
public string name;
}
class Program
{
...
1
vote
1
answer
123
views
A general-purpose decorator - correctly awaiting an awaitable type obtained from Reflection
Preamble: I was looking at how best to implement a general-purpose decorator in C# that copes with async methods. Most examples use DispatcherProxy, which only offers a synchronous Invoke. Unless NET ...
2
votes
2
answers
139
views
C# Dynamic Object call TryInvoke with an unknown array of arguments
I need to invoke a given DynamicObject with a given array of arguments. However, I am struggling on how exactly to do this.
What I would like to do:
using System.Dynamic;
using System.Reflection;
...
0
votes
2
answers
71
views
How to initialize properties using Reflection?
Given the following sample POCO class:
public class TestClass
{
public int Id { get; init; }
public TestClass() {}
}
How can I initialize the Id property using Reflection?
The result should ...
-2
votes
2
answers
141
views
Recursively instantiating the classes inside an object
I want to activate, via reflection automatically and recursively all the nested classes inside a class I am instantiating. Here is the template of what I am trying to do, but I do not know how to make ...
0
votes
3
answers
202
views
How to check if a given value exists in list of constants
I have a class called Department which contains some constants, I need a method to check if a given value exists in this constants list.
public static class Department
{
#region Public Constants
...
0
votes
1
answer
100
views
Late binding in VB.net and a "last chance" method?
I have several (very old and code frozen) VB.Net class libraries that have dozens of classes derived from a single base class, cBaseRow. The subclasses are mostly collections of properties that call ...
1
vote
0
answers
64
views
How to get the list of all loaded assemblies ordered by dependencies
I know AppDomain.GetAssemblies can be used to get all loaded assemblies of the current app but there is no guarantee of order remarked there. I need that list but also need to make sure that ...
0
votes
0
answers
143
views
Remove Route annotation from an action in ASP.NET Core 8
In my ASP.NET Core 8 project, I added a reference to a nuget package containing the following controller:
public class ShoppingCartController : Controller
{
// ...
[HttpGet]
[Route("...
1
vote
1
answer
59
views
List all property names in classes and sub classes - problem including ilist 'Object does not match target type.'
I want to get all property names in a class and list(of class) in deep hierarchy.
I can get property names of Address and Address.Floors but with Address.Floors.Peoples I get an error that the object ...
1
vote
0
answers
48
views
VB.NET programming patterns for late binding of parsers for varying dataset file formats
synopsis
It's been a long time since writing VB.NET code and since then .NET CORE has been created and grown. So quite some time. And programming practices and patterns have evolved without my keeping ...
0
votes
2
answers
161
views
Using reflection to set an entry value in generic dictionary
I am attempting to use System.Reflection to modify values in a generic dictionary in which I do not know the types of the IKey and IValue. I am able to set the values of the Dictionary.Entry.value ...
0
votes
1
answer
79
views
Map fields dynamically in c#.Need an alternative to Reflection
I have below fields in my class
[JsonProperty("LTR-AMT-1")]
public string? LTRAMT1 { get; set; }
[JsonProperty("LTR-AMT-2")]
public string? LTRAMT2 { get; set; }
[JsonProperty(...
0
votes
0
answers
98
views
How do I transform a function delegate to IL code
This question is a follow up on Unhandled exception: System.IO.FileNotFoundException: The file or assembly "System.Private.CoreLib" could not be found in custom generated assmbly.
I ...
2
votes
1
answer
1k
views
AssemblyLoadContext and dependency resolving when assemblies and the calling app have different versions of the same dependency
I believe I've exhausted the answers and comments on SO related to this topic. Full source code for the project I used to demonstrate the problem can be found here: https://github.com/jchristn/...
0
votes
1
answer
78
views
Why can't PowerShell find .NET libraries from loaded assembly?
I have a .NET assembly that I retrieve from a service that gets put into a byte[]. I then load this assembly using reflection. Despite this being successful, PowerShell will give an error saying it is ...
0
votes
0
answers
33
views
accessing indexed property of object? [duplicate]
so i have an object "Tabs" that i would to read the properties of. in this case, we have a count of 3, so there are 3 tabs. specifically trying to get the HeaderText property of the very ...
2
votes
0
answers
138
views
How to detect classes marked with file modifier
In C# 11, the new file type modifier was introduced.
I was looking for a way to detect such non-public type using reflection, however, I have not find any "good" way to do so.
There is no Is…...
3
votes
1
answer
165
views
How to instantiate a class from a running process
The scenario: We wrote a small program, let's call it MyLittleProggy.exe, that can be launched and it will continue to run, basically just idling.
MyLittleProggy.exe contains a public class Class1
Now ...
0
votes
1
answer
63
views
C# Reflection emit invalid program when calling emited method from generic type that implements interface
I am generating a proxy dynamically in C# to wrap over a derived type from an interface, intercepting the methods from the interface and calling another method with the intercepted parameters.
My ...
0
votes
0
answers
56
views
An analog of Assembly.LoadFrom for AssemblyLoadContext
I use the Assembly.LoadFrom(dll) method to load the assembly with tests and get a list of tests in the project from it
public IEnumerator<string> GetEnumerator()
{
Assembly.LoadFrom(...
0
votes
1
answer
80
views
Dynamic expression incorrect number of parameters
I am using reflection and dynamic expressions to auto-populate a class and all its properties with fake data using bogus, my code looks like:
static Faker<T> AutoInferRules<T>() where T ...
1
vote
1
answer
88
views
Why does EventInfo.EventHandlerType return a nullable Type value?
The EventHandlerType property of the System.Reflection.EventInfo class is declared as returning a nullable Type?:
EventInfo.EventHandlerType Property
public virtual Type? EventHandlerType { get; }
...
0
votes
0
answers
118
views
C# Assembly is deactivated and cannot run any code error
Hopefully I explain this well enough.
When I try to access a dll I created (I access this dll via reflection)I get an error at a certain point saying its "deactivated".
I can run code from ...
-1
votes
3
answers
701
views
C# PropertyInfo.GetValue() when the property is a SortedSet<T>
I refactored the code and added comments to make it clear. See pic of code and result
UPDATE: I know the info I provided was a bit flawed so I edited the question. Nevertheless, how does one get the ...
0
votes
1
answer
92
views
Retrieve value from Model attributes raise "Object does not match target type" in C#
I am trying to retrieve attribute information from the Identity User Model. Data in the User model either came from a database or API and here is the
From the controller, I am retrieving data by ...
1
vote
1
answer
259
views
Is Microsoft Documentation incorrect for Assembly.LoadFile method?
I have this code which tries to load all assemblies in a folder and then for each assembly, also check if all of its dependencies too can be found in the same folder.
private static string ...
0
votes
2
answers
392
views
How to parse a method invocation in IL bytes?
I know how to use the System.Reflection API, but not how to parse IL (Intermediate Language), though I do know how to parse x86 opcodes for what that's worth.
Now I want to find all the methods in an ...
-2
votes
1
answer
145
views
Why does GetType() give me the derived type but GetType().GetProperties() gives me the base class properties? [closed]
I'm trying to avoid code duplication by having a function in a base class that creates a list of properties which implement a specific interface.
When I call the function it doesn't work as I hope.
...
-1
votes
1
answer
79
views
How to set value to protected property derived from abstract class? C#
I have a class which was derived from base class. To make some manipulations with it in unit tests I would like to change some protected properties with reflection. But when I'm trying to do this I ...
1
vote
1
answer
57
views
How can I add value to a reflected variable?
I want to add value to a value I got through reflection. The += does not work and SetValue does not fit my needs. *
Instance.Field does exist but this would not be good for my script(For reasons).
...
-1
votes
1
answer
97
views
How do I fix this bug in Unity C# with getting a variable through reflection? [closed]
I have this code to find my variable:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Reflection;
...
3
votes
1
answer
116
views
Why does integer '0' IEquatable.Equals return true against a null object when called through reflection but false when called on the integer?
Calling IEquatable.Equals from an integer 0 to test against a null object should return false but when using reflection it returns true. Given the following code, I would expect for iEquals and equals ...
0
votes
1
answer
133
views
Get original unique C# Enum objects when they share an integral value
If I have a C# enum where two objects map to the same integral value, is there any possible way to get each unique Enum object? Every method I have tried returns only one of the objects - which is ...
0
votes
1
answer
72
views
Xamarin.Forms: Get PropertyInfo from Binding information
In XAML I define some elements, e.g. ToolbarItems:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" ...
1
vote
1
answer
212
views
Entity Framework Core how to instantiate DBContext using reflection?
I have a NET 7 console app using EF core, I have a situation where I need to instantiate my custom DB Context classes using reflection passing in a DBContextOptions parameter.
I am getting the ...
1
vote
2
answers
349
views
How to update the property value (Database field) of T Entity (Database Table) from the DbContext? (save decrypted data back to the DbContext)
How to iterate through DbContext and decrypt certain fields
I am using this StackOverflow answer as a guide:
Database Table Export to CSV using EntityFramework DbContext .NET 6
We have very a legacy ...
1
vote
0
answers
301
views
How to prevent RuntimeMethodInfo objects from leaking memory?
I’m comparing two snapshots taken using dotMemory profiler, and I find that there are a number of RuntimeMethodInfo objects that survived through two snapshots. These objects represent methods of a ...
0
votes
0
answers
112
views
C# Compilation to be done from a EDITOR with JSON Support (Serialize and Deserialize)
I am here after spending weeks trying to resolve the below issue.
I have c# code which is written in the front end editor.
We use Monaco Editor on the front end to write the code.
That code is ...
-1
votes
1
answer
65
views
C# Static Constructors for Inherited Classes
I have a project with around 100k XML configuration files for managing test equipment. In order to make the files easier to update, I want to be able to port them around to Excel, Access, etc. I've ...
0
votes
0
answers
17
views
Text existence of covariant property without generating AmbiguousMatchException
My base view model uses Type.GetProperty to verify that a given property (for which we are about to raise PropertyChanged) actually exists in the class.
It works great. But if you test for the ...