lundi 16 octobre 2023

Static member does not have a most specific implementation in the interface [closed]

I've been trying to create a solution for a SQS & .Net solution. Also, I'm new working with SQS and I watched this video Nick Chapsas YT Channel & downloaded GitHub repo used on that video. And, guess what ? dit not work on my pc :D, got this message "Static member 'IMessageHandler.MessageType' does not have a most specific implementation in the interface". I've never used abstract static on interfaces before. So, guys if any of you can help me with this error and explain why this is happenning, I'd really appreciate that because it seems no body else had this error.

I also tried more things like change reflection implementation and DI(classes, interfaces, etc). But, everything ended up worse than before.

VS: 2022 latest version 10/16/2023

Target Framework: .Net 7





dimanche 15 octobre 2023

Get a pointer to a class field inside a class in Java

public class Example {
   private String field_;
   private Stack stack_;
   public void Metod() {
       Field field = this.getField("field_"); !!!! 
   }
}

I want to save a pointer to a class field on the stack, but I don't understand how to get this pointer. I tried using getField, but it didn't work.





samedi 14 octobre 2023

Speed ​up Roslyn solution analyser

I'm developing a tool to visualize programs written in C#, including .sln, .csproj, and .cs files. It has functionality for displaying project references, class diagrams, and method diagrams. However, I've encountered a problem: it takes much more time than I expected to load a .sln file. Is there any way to speed up the process of loading .sln files, considering my specific needs? I believe it performs more actions than necessary.

I tried examining the MSBuildWorkspace class to find any properties that could make it perform fewer actions, but it didn't help.





vendredi 13 octobre 2023

Checking if a method is declared in an interface

I want to check if a method of a class is declared in an interface.

AInterface.java:

public interface AInterface {
    Object hi();
}

AClass.java:

public class AClass implements AInterface {
    public String hi() {
        return "hello!";
    }
}

Main.java:

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        for (Method md : AClass.class.getDeclaredMethods()) {
            System.out.println("Declaring class: " + md.getDeclaringClass().getName());
            System.out.println("Return type: " + md.getReturnType().getName());
            System.out.println("Name: " + md.getName());
            System.out.println();
        }
    }

}

md.getDeclaringClass() returns the same class for both methods. So how do I check which one is in the class?





C# Serialize Generic Wrapper class

I'm trying to implement JSON serialization for an Optional wrapper class. I want the serialization to be akin to a nullable type, so:

"myOptional": null

for null, and

"myOptional": 2

or

"myOptional": { . . . }

for objects.

This is fine until I get to a wrapped object, I don't know how to tell the serializer to "serialize this object like you normally would". The Microsoft docs lead me to believe that I have to implement JsonConverterFactory, but I don't know how to tell the writer to serialize the inner object automatically.

public class OptionalTypeJsonConverterFactory : JsonConverterFactory
{
    public override bool CanConvert(Type typeToConvert)
    {
        return typeToConvert
            .GetGenericArguments()[0].IsClass;
    }

    public override JsonConverter CreateConverter(
        Type type,
        JsonSerializerOptions options
    )
    {
        return (JsonConverter)Activator.CreateInstance(
            type: typeof(OptionalJsonConverter<>).MakeGenericType(type.GenericTypeArguments[0]),
            bindingAttr: BindingFlags.Instance | BindingFlags.Public,
            binder: null,
            args: new object[] { options },
            culture: null
        )!;
    }

    private class OptionalJsonConverter<T> : JsonConverter<Optional<T>>
        where T : struct
    {
        public OptionalJsonConverter(JsonSerializerOptions options) { }

        public override Optional<T>? Read(
            ref Utf8JsonReader reader,
            Type typeToConvert,
            JsonSerializerOptions options
        )
        {
            throw new NotImplementedException();
        }

        public override void Write(
            Utf8JsonWriter writer,
            Optional<T> optional,
            JsonSerializerOptions options
        )
        {
            if (optional.IsNone())
            {
                writer.WriteNullValue();
            }
            else
            {
                // don't know what to put here. I want to serialize objects as if the wrapper was never there.
            }
        }
    }
}

How should I go about achieving this? As an example, if Optional<T> has T = MyClass and MyClass is

{
  public int ID { get; set; } = 2
  public string Name { get; set; } = "myname"
}

then I want this to serialize to

"optionalofMyClass": 
{
  "ID": 2,
  "Name": "myname"
}




Java: reflection on grandchild class attributes

I'm facing an issue while trying to use Java reflection.

I have a PermanentObject class, that implements a general method to turn an object into a JSON string (the syntax is not yet completely implemented so glide past it)

This is the code of the method:

@Override
public String toFileFormat() {
        String info = "";
        
        try {
            
            Class myClass = this.getClass();
            Class mySuperclass = myClass.getSuperclass();
            Field[] superclassFields = mySuperclass.getDeclaredFields();
            Field[] myFields = myClass.getDeclaredFields();
            
            info += "{\n";
            
            for (int i = 0; i < superclassFields.length; i++) {
                myFields[i].setAccessible(true);
                info += "\"" + superclassFields[i].getName() + "\":\"" + superclassFields[i].get(this) + "\",\n";
            }
            
            for (int i = 0; i < myFields.length; i++) {
                myFields[i].setAccessible(true);
                info += "\"" + myFields[i].getName() + "\":\"" + myFields[i].get(this) + "\"";
                if (i < myFields.length - 1) info += ",";
                info += "\n";
            }
            
            info += "}";
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return info;
}

I then have a User abstract subclass that extends PermanentObject, and a Professor subclass that extends User.

When invoking the method tho I get this error: java.lang.IllegalAccessException: class esercizio2.permanence.PermanentObject cannot access a member of class esercizio2.logic.users.User with modifiers "private"

I was wondering why this is

I can solve the problem by implementing the method in the User class rather than in the PermanentObject class, but this leads to duplication of code since there are other PermanentObject subclasses that will need to use this.

Thanks for the help :>





mercredi 11 octobre 2023

How can I dynamically register DbSets into DbContext and add a row to the table?

I have too many entities and registering everyone of them is giving me a headache not to mention the load to instantiate the dbcontext. so I tried to dynamically register the dbsets.

To dynamically register DbSets into DbContext I used the article in this link: text I added an extension as a file:

public static class ModelBuilderExtensions
{
    public static void RegisterAllEntities<BaseModel>(this ModelBuilder modelBuilder, params Assembly[] assemblies)
    {
        IEnumerable<Type> types = assemblies.SelectMany(a => a.GetExportedTypes()).Where(c => c.IsClass && !c.IsAbstract && c.IsPublic &&
          typeof(BaseModel).IsAssignableFrom(c));
        foreach (Type type in types)
            modelBuilder.Entity(type);
    }
}

then I made an abstract class called BaseEntity that all my entities implimented.

and inside the ApplicationDbContext I overrode the OnModelCreating method:

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            var entitiesAssemblyInt = typeof(BaseEntity<int>).Assembly;
            var entitiesAssemblyGuid = typeof(BaseEntity<Guid>).Assembly;
            modelBuilder.RegisterAllEntities<BaseEntity<int>>(entitiesAssemblyInt);
            modelBuilder.RegisterAllEntities<BaseEntity<Guid>>(entitiesAssemblyGuid);
        }

it works perfectly in migration but when I want to use the dbcontext in my commandHandler to add a row to my table named client I don't know how to call client from context.

context.Client.Add(client);

doesn't work because I didn't register the dbsets directly into dbcontext and there is no Client in the context.