检查是否存在具有特定字段名称的Obj? [英] Checking if an Obj exists with specific fieldname?

查看:63
本文介绍了检查是否存在具有特定字段名称的Obj?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望任何人都可以指向正确的目的地,我试图弄清楚如何检查具有特定字段名的特定对象的存在,希望对您有所帮助!这就是我的想象:

伪代码:

Hope anyone could point to the right destination, I am trying to figure out how I can check for the existence of a specific object with a specific fieldname, hope you can help! this is how I imagined it:

pseudocode:

if ((fileReaderObj with field name == test)== null)
   {
     Filereader fileReaderObj = new Filereader(test2);
   }
   else 
   {
    // do something else 
    Console.WriteLine("Hey");
   } 



换句话说:

创建对象时,我为对象中的公共字段分配了特定的名称:



Said in other words:

When i create the object I assign a specific name to a public field in the object:

Filereader fileReaderObj = new Filereader(test2);





when checking for object, how could I check if an object with that field name already exists?

推荐答案

这里有一个对源代码和编译代码的基本误解.您在源代码中为对象指定的字段名称仅在编码时具有的含义 –您称变量"fileReaderObj"仅在编译时具有相关性,而在运行时该信息不存在. br/>
听起来您的工作听起来像可以使用现有的实体管理系统(例如NHibernate)来代替,但是如果您自己这样做是有充分的理由的:您需要存储运行时引用,无论是在对象中还是在每次尝试获取一个的哈希映射中,您都可以查看它.例如:
There is a fundamental misunderstanding of source vs compiled code here. The field name you give an object in source only has meaning at coding time – the fact you called that variable ''fileReaderObj'' is only relevant at compile time, at runtime that information is not present.

What you''re doing sounds like it could be replaced by using an existing entity management system (like NHibernate), but if you are doing it yourself for good reason: you need to store a runtime reference, either in the object or in a hash map that you look up into whenever you try to get one. For example:
Dictionary<string, FileReader> cache = new Dictionary<string, FileReader>();

FileReader GetFileReader(string key){
 FileReader r;
 if(!cache.TryGetValue(key, out r)) cache[key] = r = new FileReader(key);
 return r;
}

void SomeMethod(){
 // instead of this:
 // FileReader fileReaderObj = new FileReader(test);
 // use this:
 FileReader fileReaderObj = GetFileReader("test");
}



请注意,键在运行时处设置为字符串,而不是字段名称. (如果FileReader需要该对象中的信息,则可能还需要根据它的测试"进行传递.)



Note that the key is set at runtime to a string, not a field name. (Depending on what ''test'' is you might need to pass it as well, if the FileReader needs information within that object.)


您的意思是是否存在文件句柄打开一个特定的文件?因为当您尝试打开一个已打开的文件时,会收到IO异常,该异常指示该文件由于已被使用而无法打开.
Do you mean whether there''s a file handle open for a specific file? Because when you''re trying to open an already opened file you get an IO Exception that indicates that the file could not be opened because it''s already being used.


如果即将读取文本文件..这是您的方法.如果是位图,则可以使用bmpImg.Save()进行创建.而对于存在验证,则需要使用FILE.EXISTS()方法.

If you are about to read text file.. here is the way you do. if its bitmap you can use bmpImg.Save() for creation..whereas for existance verification FILE.EXISTS() method you need to use.

if (system.IO.FILE.Exists(filePath))
    //File.OpenRead(); read file body
else
  //File.CreateText(); Create file and write text into it.


这篇关于检查是否存在具有特定字段名称的Obj?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆