Compare commits
1 Commits
0a7f6eb362
...
249a2f9ce3
| Author | SHA1 | Date | |
|---|---|---|---|
| 249a2f9ce3 |
@@ -1,14 +1,79 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Convention.RScript.Variable.CStyle
|
namespace Convention.RScript.Variable.CStyle
|
||||||
{
|
{
|
||||||
public class CScriptRScriptVariableGenerater : RScriptInjectVariableGenerater
|
public class CScriptRScriptVariableGenerater : RScriptInjectVariableGenerater
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取类型的友好显示名称,支持泛型、数组等复杂类型
|
||||||
|
/// </summary>
|
||||||
|
public static string GetFriendlyName(Type type)
|
||||||
|
{
|
||||||
|
if (type == null) return null;
|
||||||
|
|
||||||
|
// 处理泛型类型
|
||||||
|
if (type.IsGenericType)
|
||||||
|
{
|
||||||
|
// 特殊处理可空类型(Nullable<T>)
|
||||||
|
if (type.IsNullable())
|
||||||
|
{
|
||||||
|
return $"{GetFriendlyName(type.GetGenericArguments()[0])}?";
|
||||||
|
}
|
||||||
|
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
// 获取类型基础名称(移除 `1, `2 等后缀)
|
||||||
|
string baseName = type.Name.Contains('`') ? type.Name[..type.Name.IndexOf('`')] : type.Name;
|
||||||
|
sb.Append(baseName);
|
||||||
|
sb.Append('<');
|
||||||
|
|
||||||
|
// 递归处理泛型参数
|
||||||
|
Type[] args = type.GetGenericArguments();
|
||||||
|
for (int i = 0; i < args.Length; i++)
|
||||||
|
{
|
||||||
|
if (i > 0) sb.Append(", ");
|
||||||
|
sb.Append(GetFriendlyName(args[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.Append('>');
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理数组类型
|
||||||
|
if (type.IsArray)
|
||||||
|
{
|
||||||
|
string elementName = GetFriendlyName(type.GetElementType());
|
||||||
|
int rank = type.GetArrayRank();
|
||||||
|
return rank == 1 ? $"{elementName}[]" : $"{elementName}[{new string(',', rank - 1)}]";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理指针类型
|
||||||
|
if (type.IsPointer)
|
||||||
|
{
|
||||||
|
return $"{GetFriendlyName(type.GetElementType())}*";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理引用类型(ref/out 参数)
|
||||||
|
if (type.IsByRef)
|
||||||
|
{
|
||||||
|
return $"{GetFriendlyName(type.GetElementType())}&";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 普通类型直接返回名称
|
||||||
|
return type.Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 判断是否为可空类型
|
||||||
|
/// </summary>
|
||||||
|
public static bool IsNullable(Type type)
|
||||||
|
{
|
||||||
|
return type.IsGenericType &&
|
||||||
|
type.GetGenericTypeDefinition() == typeof(Nullable<>);
|
||||||
|
}
|
||||||
|
|
||||||
public static string GetTypename(Type type)
|
public static string GetTypename(Type type)
|
||||||
{
|
{
|
||||||
if (type == typeof(int))
|
if (type == typeof(int))
|
||||||
@@ -21,12 +86,7 @@ namespace Convention.RScript.Variable.CStyle
|
|||||||
return "bool";
|
return "bool";
|
||||||
if (type == typeof(void))
|
if (type == typeof(void))
|
||||||
return "void";
|
return "void";
|
||||||
if (type.IsEnum)
|
return GetFriendlyName(type).Replace('`', '_');
|
||||||
return type.FullName.Replace('`', '_');
|
|
||||||
if (type.IsClass)
|
|
||||||
return type.FullName.Replace('`', '_');
|
|
||||||
var name = type.Name.Replace('`', '_');
|
|
||||||
return name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int m_layer = 0;
|
private int m_layer = 0;
|
||||||
@@ -116,7 +176,9 @@ namespace Convention.RScript.Variable.CStyle
|
|||||||
|
|
||||||
protected override string WritePageHead(Type currentType)
|
protected override string WritePageHead(Type currentType)
|
||||||
{
|
{
|
||||||
return $"#include\"{GetFilename(currentType.BaseType)}\"";
|
if (currentType.BaseType != null)
|
||||||
|
return $"#include\"{GetFilename(currentType.BaseType)}\"";
|
||||||
|
return string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string GetFilename(Type currentType)
|
public override string GetFilename(Type currentType)
|
||||||
|
|||||||
Reference in New Issue
Block a user