lundi 21 août 2017

Error in dynamic mapping: Missing type mapping or unsupported mapping

I have a data data something like this:

Public class Columns
{
Public String ColumnSource {get; set;}
Public public string TargetDb {get; set;}
Public DbType Type {get; set;}
}

Public class Table
{
Public string Name {get; set;}
Public IList <Column> Columns {get; set;}
}


Var DictionariesData = new Table ();

I use the System.Reflection.Emit.TypeBuilder class to generate a type at run time.

Use Dapper to get the data without a database and generate a variable with the type of the table, for example:

Var result = connection.Query (typeof (classGerada), "select * from Table");

Now I'd like to do a mapping from classGenerated to ClassGenerated2, one difference of the two is once for two columns and all things as columns.

I'm trying to do this to use AutoMapper (it's not like doing in another library, I accept suggestion). I am using the following mapping:

AutoMapper.Mapper.Initialize (f => f.CreateMap (targetClass, sourceClass)
.ForMember ("Sequential", m => m.MapFrom ("TableId")));

When I run

Var resultMap = Mapper.Map (result, sourceClass);

I have the following error:

Map configuration of missing type or mapping not supported.

Mapping types: IEnumerable1 -> Type System.Collections.Generic.IEnumerable1 [[System.Object, mscorlib, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089]] -> System.Type

*sourceClass is the type generated at runtime from the data dictionary.

Does anyone know what's wrong? Or what would one do?





Type.GetType(typeName) as a generic type? [duplicate]

This question already has an answer here:

I need to build a function to return the deserialized object of a JSON string, the type name is stored in the database:

Type jsonType = Type.GetType(log.JSONClassName);

so I need to do something like this:

var obj = AuditHelper.GetJsonType<jsonType>(log.ObjectJson);

AuditHelper.GetJsonType method:

 public static T GetJsonType<T>(string json)
        {
            var logObject = JsonConvert.DeserializeObject<T>(json);
            return logObject;
        }





C# - Get Type by string without specifiying full namespace

Currently, when using Assembly.GetType(string typeName) I have to specify the full namespace name, so if I had a class called MyClass within a namespace - MyNamespace.CoolClasses, to use the Assembly.GetType method I would have to specify the full namespace before the class as follows: "MyNamespace.CoolClasses.MyClass".
I would like to know if there is an existing method similar to the one I mentioned where I don't need to specifiy the full namespace name. So, if we go back to my example, all I'd need to write to get the type is Assembly.GetType("MyClass")





dimanche 20 août 2017

Redirect input from annotation [duplicate]

This question already has an answer here:

Is there a way that I can give an annotation an method as an input. Then use the value returned from it to pass to the annotation ?

public String fnc(String value){

   //do something
   return ret;
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface MAnon{
    String cond = fnc(cond);
}

@MAnon(fnc("Something")) //does not work
@MAnon("Something")
public void foo(){ ... }

public void bar(){
    //reflection call
   MAnon a = ...getDeclaredAnnotations()[0]
   String foo = a; //may not be the same as what is typed
}





How can I pass the unknown type of an instance to a generic method?

I have a method with the signature:

public static void MyMethod<T>()

I want to call it passing the generic type of some arbitrary variable:

var myFoo = new Foo(); //I do not know that this is Foo
var genericType = ???; //What should I write here?
MyMethod<genericType>();

How can I pass the generics of an instance in a method?


What have I tried? A bunch of things. An example, of one that does not compile:

var genericType = myFoo.GetType();





How to call Action

In this example code i am trying to invoke a anonymous action from il generator. I am not sure if and how i can load the reference to the delegate and how to call it. I can do it if the OnFunctionCall is a static method not property.

public delegate void TestDelegate();

public static class ExampleOne
{
    public static Action<string, bool> OnFunctionCall
        => (message, flag) => Console.WriteLine("Example");
}

public static class ExampleTwo
{
    public static TType CreateDelegate<TType>(Action<string, bool> onFunctionCall)
        where TType : class
    {
        var method = new DynamicMethod($"{Guid.NewGuid()}", typeof(void), Type.EmptyTypes, typeof(TType), true);

        ILGenerator il = method.GetILGenerator();

        // loading the first string argument
        il.Emit(OpCodes.Ldstr, method.Name);

        // not sure here how to load boolean value to the stack
        il.Emit(OpCodes.Ldc_I4_0);

        // this line doesn't work
        // example two has no idea about ExampleOne
        // is it possible to load the reference of the Action<string, bool> to the stack and call it ?
        il.Emit(OpCodes.Call, onFunctionCall.Method);

        il.Emit(OpCodes.Ret);

        return method.CreateDelegate(typeof(TestDelegate)) as TType;
    }
}

public class Program
{
    public static void Main(string[] args)
        => ExampleTwo
            .CreateDelegate<TestDelegate>(ExampleOne.OnFunctionCall)
            .Invoke();
}





Mirroring method in C#

Is there any ways I could use, to mirror any method from another class? i.e.

public class MyMethods
{

    public static double Func_1
    {


    }
    public static double Func_2
    ....
    ....
    ....

}





public class Reflector
{
    // any call to this class, should be redirected to MyMethods
    public SOMETHING_AUTOLOADER_OR_LIKE_THAT{
        ...
    }

}

as i could call Reflector.Func_888(); , and if that method is not found there, then it should search in MyMethods and execute that.