有没有办法 "define" LeMP 中的宏接受 Type 文字输入
Is there way to "define" a macro in LeMP accepting the Type literal input
我正在使用 Extended CSharp 中的 LeMP v2.8.1,并使用以下快捷方式定义和初始化 class 中的字段:
compileTimeAndRuntime
{
public partial class DI : IResolver
{
replace (T => ConcurrentDictionary<Type, Expression<Func<IResolver, object>>>) { public T Registrations = new T(); }
replace (T => ConcurrentDictionary<Type, Func<IResolver, object>>) { public T DelegateCache = new T(); }
}
}
除了 public
和 internal
修饰符外,替换模式非常相似。但是让我们暂时跳过它们。
我的问题是有没有办法将这个 replace
模式分解为一个 define
宏,这样我就不需要重复这个模式了。
我尝试了类似 define New($T, $F) { $T $F = new $T(); }
的方法,但是当它与这样的类型参数一起使用时它会吐出错误 New(ConcurrentDictionary<Type, Expression<Func<IResolver, object>>>, Registrations)
。
对我有用:
compileTimeAndRuntime
{
define New($T, $F) { $T $F = new $T(); }
public partial class DI : IResolver
{
public New(ConcurrentDictionary<Type, Expression<Func<IResolver, object>>>, Registrations);
internal New(ConcurrentDictionary<Type, Func<IResolver, object>>, DelegateCache);
}
}
// Generated from Untitled.ecs by LeMP 2.8.2.0.
public partial class DI : IResolver
{
public ConcurrentDictionary<Type, Expression<Func<IResolver, object>>> Registrations = new ConcurrentDictionary<Type, Expression<Func<IResolver, object>>>();
internal ConcurrentDictionary<Type, Func<IResolver, object>> DelegateCache = new ConcurrentDictionary<Type, Func<IResolver, object>>();
}
这是一个更有趣的版本:
// Matches variable declarations and replaces __var with the type name.
// Useful to avoid repeating the type twice when declaring fields in classes.
[Passive] // avoids warning on variable declarations that don't match
define #var(__var, $name = new $T($(..args)) { $(..initializers) }) {
$T $name = new $T($args) { $initializers };
}
public partial class DI : IResolver
{
public __var Registrations = new ConcurrentDictionary<Type, Expression<Func<IResolver, object>>>();
internal __var DelegateCache = new ConcurrentDictionary<Type, Func<IResolver, object>>();
private __var _array = new int[4] { 0, 10, 20, 30 };
}
// Generated from Untitled.ecs by LeMP 2.8.2.0.
public partial class DI : IResolver
{
public ConcurrentDictionary<Type, Expression<Func<IResolver, object>>> Registrations = new ConcurrentDictionary<Type, Expression<Func<IResolver, object>>>();
internal ConcurrentDictionary<Type, Func<IResolver, object>> DelegateCache = new ConcurrentDictionary<Type, Func<IResolver, object>>();
private int[] _array = new int[4] {
0, 10, 20, 30
};
}
这是基于我的知识 T x = y
有一个形式为 #var(T, x = y)
的 Loyc 树,但是 replace
宏允许你在不知道这些的情况下做同样的事情:
replace (
{ __var $name = new $Type($(..args)) { $(..initializers) }; } =>
{ $Type $name = new $Type($(..args)) { $(..initializers) }; });
抱歉花了这么长时间才回答。
我正在使用 Extended CSharp 中的 LeMP v2.8.1,并使用以下快捷方式定义和初始化 class 中的字段:
compileTimeAndRuntime
{
public partial class DI : IResolver
{
replace (T => ConcurrentDictionary<Type, Expression<Func<IResolver, object>>>) { public T Registrations = new T(); }
replace (T => ConcurrentDictionary<Type, Func<IResolver, object>>) { public T DelegateCache = new T(); }
}
}
除了 public
和 internal
修饰符外,替换模式非常相似。但是让我们暂时跳过它们。
我的问题是有没有办法将这个 replace
模式分解为一个 define
宏,这样我就不需要重复这个模式了。
我尝试了类似 define New($T, $F) { $T $F = new $T(); }
的方法,但是当它与这样的类型参数一起使用时它会吐出错误 New(ConcurrentDictionary<Type, Expression<Func<IResolver, object>>>, Registrations)
。
对我有用:
compileTimeAndRuntime
{
define New($T, $F) { $T $F = new $T(); }
public partial class DI : IResolver
{
public New(ConcurrentDictionary<Type, Expression<Func<IResolver, object>>>, Registrations);
internal New(ConcurrentDictionary<Type, Func<IResolver, object>>, DelegateCache);
}
}
// Generated from Untitled.ecs by LeMP 2.8.2.0.
public partial class DI : IResolver
{
public ConcurrentDictionary<Type, Expression<Func<IResolver, object>>> Registrations = new ConcurrentDictionary<Type, Expression<Func<IResolver, object>>>();
internal ConcurrentDictionary<Type, Func<IResolver, object>> DelegateCache = new ConcurrentDictionary<Type, Func<IResolver, object>>();
}
这是一个更有趣的版本:
// Matches variable declarations and replaces __var with the type name.
// Useful to avoid repeating the type twice when declaring fields in classes.
[Passive] // avoids warning on variable declarations that don't match
define #var(__var, $name = new $T($(..args)) { $(..initializers) }) {
$T $name = new $T($args) { $initializers };
}
public partial class DI : IResolver
{
public __var Registrations = new ConcurrentDictionary<Type, Expression<Func<IResolver, object>>>();
internal __var DelegateCache = new ConcurrentDictionary<Type, Func<IResolver, object>>();
private __var _array = new int[4] { 0, 10, 20, 30 };
}
// Generated from Untitled.ecs by LeMP 2.8.2.0.
public partial class DI : IResolver
{
public ConcurrentDictionary<Type, Expression<Func<IResolver, object>>> Registrations = new ConcurrentDictionary<Type, Expression<Func<IResolver, object>>>();
internal ConcurrentDictionary<Type, Func<IResolver, object>> DelegateCache = new ConcurrentDictionary<Type, Func<IResolver, object>>();
private int[] _array = new int[4] {
0, 10, 20, 30
};
}
这是基于我的知识 T x = y
有一个形式为 #var(T, x = y)
的 Loyc 树,但是 replace
宏允许你在不知道这些的情况下做同样的事情:
replace (
{ __var $name = new $Type($(..args)) { $(..initializers) }; } =>
{ $Type $name = new $Type($(..args)) { $(..initializers) }; });
抱歉花了这么长时间才回答。