我甚至不知道它是否被称为别名,但无论如何让我继续.
你知道C#中的System.String类型是如何用"字符串"排序"别名"的?在Visual Studio中,"string"是小写和蓝色文本.我想对一些冗长的班级名字这样做.有些都像"RelocatedPlantProductReleaseItem".我无法更改它的名称,但这么长的名字确实使代码啰嗦.我希望能够使用"Rppr"代替.所以不是这样的:
RelocatedPlantProductReleaseItem Item = new RelocatedPlantProductReleaseItem();
我会像:
Rppr Item = new Rppr();
我知道我可以创建一个名为Rppr的类,并让它继承自RelocatedPlantProductReleaseItem,但这看起来很糟糕.
此外,我上面给出的场景完全是虚构的.我只是用它来演示我想要实现的基本功能.
另外,当"string"表示System.String时,技术上称之为什么?
更新:感谢所有答案,伙计们,但我想我应该指明我希望这是一个全球性的事情.我不想在每个代码文件的顶部指定它.我想在一个地方指定它,它在整个应用程序中的任何地方都可以工作,即使在引用其程序集的其他程序集中也是如此.
只是要列出我拥有的所有信息:
总之,我要找的东西正是(直译)像这样,但兼容ASP核心(2.2)和C#的MongoDB驱动程序(2.7)。
这似乎是一个常见的要求,我很惊讶我找不到任何已经构建的东西。
这是我到目前为止所拥有的:
模型:
public class Patient
{
//comes from the client as XXXXXXXXX, RegEx: "([0-9]{9})"
//[MongoEncrypt]
public EncryptedString SocialSecurityNumber { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
属性:
[AttributeUsage(AttributeTargets.Property)]
public class MongoEncryptAttribute : BsonSerializerAttribute
{
public MongoEncryptAttribute()
{
SerializerType = typeof(MongoEncryptSerializer);
}
}
Run Code Online (Sandbox Code Playgroud)
自定义序列化程序:
public interface IMongoEncryptSerializer : IBsonSerializer<EncryptedString>{ }
public class MongoEncryptSerializer : SerializerBase<EncryptedString>, IMongoEncryptSerializer
{
private readonly string _encryptionKey;
public MongoEncryptSerializer(IConfiguration configuration)
{
_encryptionKey = configuration.GetSection("MongoDb")["EncryptionKey"];
}
public override EncryptedString Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var encryptedString …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的房产
private int clientID;
public int ClientID
{
get { return clientID; }
set { clientID = value; }
}
Run Code Online (Sandbox Code Playgroud)
我希望能够将一个字符串传递给这个属性,以便setter将它转换为我.我怎么做?
我知道如何进行转换,我只需要知道如何在不抛出类型错误的情况下传递字符串.
谢谢,
汤姆