Skip to content

Tsinswreng/CsDictMapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 

Repository files navigation

CsDictMapper

Convert between IDictionary<string,object?> and C# object.

AOT friendly, Free of reflection.

⚠️This project has no release version yet and everything may change. If you want to use it, we recommand you to clone the code and reference the source code directly.⚠️

Usage

Config the mapper

using Tsinswreng.CsDictMapper;

namespace MyProjectNamespace; // don't put things in global namespace otherwise it may not work

public  partial class Person{
	public int Age{get;set;}
	public string Name {get;set;}
}

[DictType(typeof(Person))]
//[DictType(typeof(MoreTypes))]
public partial class AppDictMapper{
	public static AppDictMapper Inst{get;} = new();

}

Use the mapper

using Tsinswreng.CsDictMapper;
// object to dictionary:
var person = new Person{Age=22, Name="Tsinswreng"};
var dict = AppDictMapper.Inst.ToDictShallowT(person);
// the dict will be like {"Age":22, "Name":"Tsinswreng"}

// assign the dictionary to object:
var person2 = new Person();
AppDictMapper.Inst.AssignShallowT(person2, dict);
// the content of `person2` will be the same as `person`

Reference this project by source code

  • Clone the repo
  • In your .csproj file:
<ItemGroup>
	<!-- reference the source generator assembly -->
	<ProjectReference Include="../Tsinswreng.CsDictMapper/proj/Tsinswreng.CsDictMapper.SrcGen/Tsinswreng.CsDictMapper.SrcGen.csproj"
		OutputItemType="Analyzer" ReferenceOutputAssembly="false"
	/>

	<!-- reference the API assembly, including Attributes, interfaces etc. -->
	<ProjectReference Include="../Tsinswreng.CsDictMapper/proj/Tsinswreng.CsDictMapper/Tsinswreng.CsDictMapper.csproj" />

</ItemGroup>

Documentation

The class attached [DictType(typeof(T))] will automatically implement the interface IDictMapperShallow

namespace Tsinswreng.CsDictMapper;
using str = string;
public  partial interface IDictMapperShallow{
	public IDictionary<Type, IDictMapperForOneType> Type_Mapper{get;set;}
	public IDictionary<str, object?> ToDictShallowT<T>(T Obj);
	public IDictionary<str, object?> ToDictShallow(Type Type, object? Obj);

	public IDictionary<str, Type> GetTypeDictShallowT<T>();
	public IDictionary<str, Type> GetTypeDictShallow(Type Type);

	public T AssignShallowT<T> (T Obj, IDictionary<str, object?> Dict);
	public object AssignShallow(Type Type, object? Obj, IDictionary<str, object?> Dict);
}


public  partial interface IDictMapperForOneType{
	public Type TargetType{get;}
	public IDictionary<str, object?> ToDictShallow(object Obj);
	public IDictionary<str, Type> GetTypeDictShallow();
	public object AssignShallow(object Obj, IDictionary<str, object?> Dict);

}

About

Convert between IDictionary<string,object?> and C# object, free of reflection

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages