繁体   English   中英

在对象类型之间切换语句

[英]Switch statement between object types

我正在尝试使用switch语句来声明变量。 根据我发送的情况,变量将是不同的对象类型。

object savedKey = null;
    switch (type)
    {
        case "RegistrationKey":
            savedKey = db.RegistrationKey.FirstOrDefault(k => k.RegistrationKey == key);
            break;
        case "ClockKey":
            savedKey = db.ClockKey.FirstOrDefault(k => k.RegistrationKey == key);
            break;
    }

我遇到的问题是,当我尝试访问模型中的列时,出现消息Cannot Resolve Symbol

var decSavedkey = ConCryptor.Decrypt(savedKey.RegistrationKey);

找不到注册密钥。 如果我声明对象类型RegistrationKey savedKey = null; 那么我没有收到错误,但是我的switch语句中的ClockKey返回了一个错误,因为我将RegistrationKey声明为对象。

这是使用saveKey的方式

if (savedKey != null)
        {
            try
            {
                var sentKey = ConCryptor.Decrypt(key);
                var decSavedkey = ConCryptor.Decrypt(savedKey.RegistrationKey);
                var today = DateTime.Now;
                // lets validate some keys
                if (sentKey == decSavedkey && DateTime.Parse(savedKey.ExpirationDate.ToString()) >= DateTime.Parse(today.Date.ToString()))
                {
                    status = true;
                }
            }
            catch (Exception e)
            {
                status = false;
            }

        }

您需要一些通用的基类或接口,这两个类都源自/实现。 作为object doesn't了解一个什么 RegistrationKey ,你enitities从数据库返回因而应当是类型Class1Class2

interface MyInterface
{
    List<Key> Keys { get; set;}
}
public class Class1 : MyInterface
{
    public List<Key> Keys { get; set;}
}
public class Class2 : MyInterface
{
    public List<Key> Keys { get; set;}
}

现在您不需要任何切换,因为实例本身知道如何解析Key

MyInterface instance = db.QueryFeature(...);
savedKey = instance.Keys.FirstOrDefault(k => k.RegistrationKey == key);

您可以创建一个接口,该接口声明ClockKeyRegistrationKey的通用属性:

public interface IKeyProps
{
    string RegistrationKey {get;set;}
    DateTime ExpirationDate {get;set;}
}

并让两种类型都实现它:

public class RegistrationKey : IKeyProps
{ /*...*/ }
public class CloseKey : IKeyProps
{ /*...*/ }

然后将savedKey变量声明为IKeyProps

IKeyProps savedKey = null;
switch (type)
{
    case "RegistrationKey":
        savedKey = db.RegistrationKey.FirstOrDefault(k => k.RegistrationKey == key);
        break;
    case "ClockKey":
        savedKey = db.ClockKey.FirstOrDefault(k => k.RegistrationKey == key);
        break;
}    

// this now works as IKeyProps declares a RegistrationKey property
var decSavedkey = ConCryptor.Decrypt(savedKey.RegistrationKey);

或者(如果您不能更改类声明),则可以将savedKey声明为dynamic 但是,然后您松开了类型安全性和IntelliSense支持等。这是一个hack,而不是一个干净的解决方案。

只需将单词“ object”更改为该表的类型名称,或者如果类型不同,则将其更改为“ dynamic”

添加继承或接口来解决此问题本质上没有错,但我认为这是没有必要的。

为将从每个DbSet项中获取的数据创建局部变量。

string registrationKey = string.Empty;
DateTime expirationDate = DateTime.Min;

switch (type)
{
    case "RegistrationKey":
        var registrationKeyItem = db.RegistrationKey.FirstOrDefault(k => k.RegistrationKey == key)
        if (registrationKeyItem != null)
        {
            registrationKey = registrationKeyItem.RegistrationKey;
            expirationDate = registrationKeyItem.ExpirationDate;
        }
        break;
    case "ClockKey":
        var clockKeyItem = db.ClockKey.FirstOrDefault(k => k.RegistrationKey == key);
        if (clockKeyItem != null)
        {
            registrationKey = clockKeyItem.RegistrationKey;
            expirationDate = clockKeyItem.ExpirationDate;
        }
        break;
}

然后使用检索到的数据调用逻辑。 基本上,保持简单。

var sentKey = ConCryptor.Decrypt(key);
var decSavedkey = ConCryptor.Decrypt(registrationKey);
var today = DateTime.Now;
// lets validate some keys
if (sentKey == decSavedkey && 
    DateTime.Parse(expirationDate.ToString()) >= 
    DateTime.Parse(today.Date.ToString()))
{
     status = true;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM