mardi 5 juillet 2022

Get type of decorated parameter in attribute

Having an attribute like this,

[AttributeUsage(AttributeTargets.Parameter)]
public class MyParamAttribute : Attribute { .. }

is there a way to get the type of the decorated parameter in the attribute implementation?

public void MyMethod([MyParam] string param) { .. }
[AttributeUsage(AttributeTargets.Parameter)]
public class MyParamAttribute : Attribute
{
    public Type ParamType => ...?
}

I wonder if there's a subtle way to get the type of the parameter apart from passing it directly by constructor.

[AttributeUsage(AttributeTargets.Parameter)]
public class MyParamAttribute : Attribute
{
    public MyParamAttribute(Type type) => ParamType = type;

    public Type ParamType { get; }
}

// ...
public void MyMethod([MyParam(typeof(string))] string param) { .. }

Thanks in advance.





C# get all underlaying properties of with a specific name via reflection [duplicate]

I would like to search a class (and it's properties which are also instances of classes) for a specific property(name). In the current code i'm only getting it's own property.

example classes:

    public class Root
{
    public List<CodeTypes> Codes { get; set; }
    public Descendant Child { get; set; }
}

public class Descendant
{
    public List<CodeTypes> Codes { get; set; }
    public AnotherDescendant SomeChild { get; set; }
}

public class AnotherDescendant
{
    public List<CodeTypes> Codes { get; set; }
}

public enum CodeTypes
{
    Ok,
    NotOk,
    DontNow
}

In my current solution i'm only getting the list from the Root class.

 var MyCodes = itemToClean.GetType().GetProperties().Where(p => p.Name.Equals("Codes", StringComparison.OrdinalIgnoreCase));

I'm looking for a GetProperties which returns all the List lists from the descendants of a class. Can this be done with reflection (without recurrance)?





lundi 4 juillet 2022

How to set default value to annotation variable as the class type of the variable annotated?

I have a custom annotation with a single variable.

I use it to annotate attributes in a class and what i need is that the annotation default value for the variable, be the type of the attribute declared. Here the example:

Annotation:

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Annotation{
    Class<?> className() default ???????; // <- here i need to set something that tells my annotation to take the class of the attribute annotated
}

Class using Annotation:

public class Main {

    @Annotation
    private AnotherClass annotatedAttribute;

    //other code
}

And so what i need is that when i get the annotatedAttribute field and i get its annotation and its value of the className() variable, the default value should be the equivalent to AnotherClass.class unless i state otherwise in the declaration of the @Annotation

E.g:

@Annotation(classname= YetAnotherClass.class)

Is there a way to do this?

I saw some posts talking about an annotation processor, but in my case i don't want to generate new classes files since my class already exist and i'm fetching the field and the annotation through reflection (so i'm at runtime level)





C# is it possible to construct new object from type variable? [duplicate]

Lets say I have a Type variable. Is there a way to call the default empty constructor from the variable and return a new object of said type? If such a thing exists, is there a way to call a non-default constructor in this way?





dimanche 3 juillet 2022

How can a function get the module name of the caller?

Say I have two lua modules, like this.


Filename: my/lua/app/caller.lua
Module name: my.lua.app.caller

local reflect = require('my.lib.reflect')

-- should return "my.lua.app.caller"
reflect.get_caller_module_name()

Filename: my/lib/reflect.lua
Module name: my.lib.reflect

M = {}

function M.get_caller_module_name()
   -- magic here?
   return module_name
end

return M

Now running my.lua.app.caller should allow it to get its own module name, my.lua.app.caller. Is this doable in Lua?

(I realize that I can get the module name by inserting local module_name, _ = ... as the very first line of the caller's module, but I want to be able to do this without this without needing any special modifications to the caller's module.)





vendredi 1 juillet 2022

Dart - Get result from a method invoked using reflection

I'm trying to use dart's mirror API to dynamically invoke a function.

How can I obtain the result that's returned from the doWork method when invoking it via an InstanceMirror

class MyData {
  String someString;
}

class MyService {
  Future<MyData> doWork() async {
    print('doing work');
    return await Future(() => MyData()..someString = 'my result');
  }
}
void main() async {
  var instance = MyService();
  var mirrror = reflect(instance);
  var result = mirrror.invoke(#doWork, []);
}

I can see that "doing work" gets printed to the console so I know it's being invoked, but I'm struggling to interpret the result from the invoke function.





How to get a list of classes that extend from a base class in Kotlin?

consider following classes structure in Kotlin:

open class Base {
   ...
}

class A : Base() {
   ...
}

class B : Base() {
   ...
}

...

class Z : Base() {
   ...
}

How would I go around getting a list of ALL classes (A-Z in this particular example) that inherit from Base class?

I tried using reflection with BaseTest::class.nestedClasses and other reflection properties but they don't seem to be finding number of extending classes properly.