Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在camelCase中序列化存储C#类_C#_Google Cloud Firestore - Fatal编程技术网

在camelCase中序列化存储C#类

在camelCase中序列化存储C#类,c#,google-cloud-firestore,C#,Google Cloud Firestore,我正在使用.net将数据存储在Firestore数据库中。我使用FirestoreData和FirestoreProperty属性来控制对象序列化到DB的方式。默认情况下,C#属性是在PascalCase中的,我希望它们在camelCase中序列化。我知道我可以在FirestoreProperty属性中设置属性将被序列化的名称,但这是一项非常繁琐且容易出错的任务。在这种情况下,有没有办法将Firestore.net客户端配置为默认序列化属性 谢谢定义了两个构造函数。通过提供参数name,可以添加

我正在使用.net将数据存储在Firestore数据库中。我使用FirestoreData和FirestoreProperty属性来控制对象序列化到DB的方式。默认情况下,C#属性是在PascalCase中的,我希望它们在camelCase中序列化。我知道我可以在FirestoreProperty属性中设置属性将被序列化的名称,但这是一项非常繁琐且容易出错的任务。在这种情况下,有没有办法将Firestore.net客户端配置为默认序列化属性

谢谢

定义了两个构造函数。通过提供参数
name
,可以添加名称:

要在Firestore文档中使用的名称

因此,您可以简单地将其设置为属性,如

[FireStoreProperty("anyCase")]
public string AnyCase{ get; set; }
如果不修改基础类型,以静默方式执行此操作是不可能的。一种可能的方法是实现基于反射的,在运行时更改属性名称。您只需要为每个数据类定义一次转换器。以下是一种可能的方法:

//Sample data class
[FirestoreData(ConverterType = typeof(CamelCaseConverter<CustomCity>))]
public class CustomCity
{
    public string Name { get; set;  }
    public string Country { get; set; }
    public long Population { get; set; }
}

//Sample data class
[FirestoreData(ConverterType = typeof(CamelCaseConverter<CustomPerson>))]
public class CustomPerson
{
    public string Name { get; set;  }
    public uint Age { get; set; }
}

//Conversion of camelCase and PascalCase
public class CamelCaseConverter<T> : IFirestoreConverter<T> where T : new()
{
    public object ToFirestore(T value)
    {
        dynamic camelCased = new ExpandoObject();
        foreach (PropertyInfo property in typeof(T).GetProperties())
        {
            string camelCaseName =
                char.ToLowerInvariant(property.Name[0]) + property.Name.Substring(1);
            ((IDictionary<string, object>)camelCased)[camelCaseName] = property.GetValue(value);
        }
        return camelCased;
    }

    public T FromFirestore(object value)
    {
        if (value is IDictionary<string, object> map)
        {
            T pascalCased = new T();
            foreach (PropertyInfo property in typeof(T).GetProperties())
            {
                string camelCaseName =
                    char.ToLowerInvariant(property.Name[0]) + property.Name.Substring(1);
                property.SetValue(pascalCased, map[camelCaseName]);
            }

            return pascalCased;
        }
        throw new ArgumentException($"Unexpected data: {value.GetType()}");
    }
//示例数据类
[FirestoreData(ConverterType=typeof(CamelCaseConverter))]
公营城市
{
公共字符串名称{get;set;}
公共字符串国家{get;set;}
公共长填充{get;set;}
}
//样本数据类
[FirestoreData(ConverterType=typeof(CamelCaseConverter))]
公营海关人员
{
公共字符串名称{get;set;}
公共uint年龄{get;set;}
}
//camelCase和PascalCase的转化
公共类CamelCaseConverter:IFirestoreConverter,其中T:new()
{
公共对象到firestore(T值)
{
dynamic camelCased=新的expandooobject();
foreach(typeof(T).GetProperties()中的PropertyInfo属性)
{
字符串名称=
char.ToLowerInvariant(property.Name[0])+property.Name.Substring(1);
((IDictionary)camelCased)[camelCaseName]=property.GetValue(value);
}
返回骆驼壳;
}
来自FireStore的公共T(对象值)
{
if(值为IDictionary映射)
{
T pascalCased=新的T();
foreach(typeof(T).GetProperties()中的PropertyInfo属性)
{
字符串名称=
char.ToLowerInvariant(property.Name[0])+property.Name.Substring(1);
SetValue(pascalCased,map[camelCaseName]);
}
回归分析;
}
抛出新ArgumentException($“意外数据:{value.GetType()}”);
}
定义了两个构造函数。其中一个允许通过提供参数
name
来添加名称:

要在Firestore文档中使用的名称

因此,您可以简单地将其设置为属性,如

[FireStoreProperty("anyCase")]
public string AnyCase{ get; set; }
如果不修改基础类型,以静默方式执行此操作是不可能的。一种可能的方法是实现基于反射的,在运行时更改属性名称。您只需要为每个数据类定义一次转换器。以下是一种可能的方法:

//Sample data class
[FirestoreData(ConverterType = typeof(CamelCaseConverter<CustomCity>))]
public class CustomCity
{
    public string Name { get; set;  }
    public string Country { get; set; }
    public long Population { get; set; }
}

//Sample data class
[FirestoreData(ConverterType = typeof(CamelCaseConverter<CustomPerson>))]
public class CustomPerson
{
    public string Name { get; set;  }
    public uint Age { get; set; }
}

//Conversion of camelCase and PascalCase
public class CamelCaseConverter<T> : IFirestoreConverter<T> where T : new()
{
    public object ToFirestore(T value)
    {
        dynamic camelCased = new ExpandoObject();
        foreach (PropertyInfo property in typeof(T).GetProperties())
        {
            string camelCaseName =
                char.ToLowerInvariant(property.Name[0]) + property.Name.Substring(1);
            ((IDictionary<string, object>)camelCased)[camelCaseName] = property.GetValue(value);
        }
        return camelCased;
    }

    public T FromFirestore(object value)
    {
        if (value is IDictionary<string, object> map)
        {
            T pascalCased = new T();
            foreach (PropertyInfo property in typeof(T).GetProperties())
            {
                string camelCaseName =
                    char.ToLowerInvariant(property.Name[0]) + property.Name.Substring(1);
                property.SetValue(pascalCased, map[camelCaseName]);
            }

            return pascalCased;
        }
        throw new ArgumentException($"Unexpected data: {value.GetType()}");
    }
//示例数据类
[FirestoreData(ConverterType=typeof(CamelCaseConverter))]
公营城市
{
公共字符串名称{get;set;}
公共字符串国家{get;set;}
公共长填充{get;set;}
}
//样本数据类
[FirestoreData(ConverterType=typeof(CamelCaseConverter))]
公营海关人员
{
公共字符串名称{get;set;}
公共uint年龄{get;set;}
}
//camelCase和PascalCase的转化
公共类CamelCaseConverter:IFirestoreConverter,其中T:new()
{
公共对象到firestore(T值)
{
dynamic camelCased=新的expandooobject();
foreach(typeof(T).GetProperties()中的PropertyInfo属性)
{
字符串名称=
char.ToLowerInvariant(property.Name[0])+property.Name.Substring(1);
((IDictionary)camelCased)[camelCaseName]=property.GetValue(value);
}
返回骆驼壳;
}
来自FireStore的公共T(对象值)
{
if(值为IDictionary映射)
{
T pascalCased=新的T();
foreach(typeof(T).GetProperties()中的PropertyInfo属性)
{
字符串名称=
char.ToLowerInvariant(property.Name[0])+property.Name.Substring(1);
SetValue(pascalCased,map[camelCaseName]);
}
回归分析;
}
抛出新ArgumentException($“意外数据:{value.GetType()}”);
}

我正在寻找一种静默方式。为每个属性设置camel case属性名称非常繁琐且容易出错。我正在寻找一种静默方式。为每个属性设置camel case属性名称非常繁琐且容易出错