首页 \ 问答 \ 检查属性是否具有属性(Check if property has attribute)

检查属性是否具有属性(Check if property has attribute)

给定一个类中的属性,具有属性 - 确定它是否包含给定属性的最快方法是什么? 例如:

    [IsNotNullable]
    [IsPK]
    [IsIdentity]
    [SequenceNameAttribute("Id")]
    public Int32 Id
    {
        get
        {
            return _Id;
        }
        set
        {
            _Id = value;
        }
    }

确定例如它具有“IsIdentity”属性的最快方法是什么?


Given a property in a class, with attributes - what is the fastest way to determine if it contains a given attribute? For example:

    [IsNotNullable]
    [IsPK]
    [IsIdentity]
    [SequenceNameAttribute("Id")]
    public Int32 Id
    {
        get
        {
            return _Id;
        }
        set
        {
            _Id = value;
        }
    }

What is the fastest method to determine that for example it has the "IsIdentity" attribute?


原文:https://stackoverflow.com/questions/2051065
更新时间:2023-07-26 17:07

最满意答案

当您使用java命令从命令行运行Java应用程序时,例如,

    java some.AppName arg1 arg2 ...

该命令加载您提名的类,然后查找名为main的入口点方法。 更具体地说,它正在寻找一个声明如下的方法:

package some;
public class AppName {
    ...
    public static void main(String[] args) {
        // body of main method follows
        ...
    }
}

入门点方法的具体要求是:

  1. 该方法必须在提名类中。
  2. 该方法的名称必须是正确的大写字母“main” 1
  3. 该方法必须是public
  4. 该方法必须是static 2
  5. 该方法的返回类型必须为void
  6. 该方法必须只有一个参数,参数的类型必须为String[] 3

参数可以使用varargs语法声明; 例如String... args 。 请参阅https://stackoverflow.com/a/36803396/139985 。 String[]参数用于从命令行传递参数,并且即使您的应用程序不使用命令行参数也是必需的。

如果上述要求中的任何一个不满足,则java命令将失败并显示以下消息:

java.lang.NoSuchMethodError: main Exception in thread “main”

如果遇到此错误,请检查您是否具有main方法,并满足上述所有6项要求。


1 - 一个真正模糊的变化是当“主”中的一个或多个字符不是LATIN-1字符,但是在显示时看起来像相应的LATIN-1字符的Unicode字符。

2 - 看看为什么Java的main方法是静态的? 解释为什么该方法需要静态。

3 - String必须对应于java.lang.String而不是名为String的自定义类隐藏它。


When you use the java command to run a Java application from the command line, e.g.,

java some.AppName arg1 arg2 ...

the command loads the class that you nominated, and then looks for the entry point method called main. More specifically, it is looking for a method that is declared as follows:

package some;
public class AppName {
    ...
    public static void main(String[] args) {
        // body of main method follows
        ...
    }
}

The specific requirements for the entry point method are:

  1. The method must be in the nominated class.
  2. The name of the method must be "main" with exactly that capitalization1.
  3. The method must be public.
  4. The method must be static 2.
  5. The method's return type must be void.
  6. The method must have exactly one argument and argument's type must be String[] 3.

(The argument may be declared using varargs syntax; e.g. String... args. See this question for more information. The String[] argument is used to pass the arguments from the command line, and is required even if your application takes no command line arguments.)

If any one of the above requirements is not satisfied, the java command will fail with some variant of the message:

Error: Main method not found in class MyClass, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

Or, if you are running an extremely old version of Java:

java.lang.NoSuchMethodError: main
Exception in thread "main"

If you encounter this error, check that you have a main method and that it satisfies all 6 of the requirements listed above.


1 - One really obscure variation of this is when one or more of the characters in "main" is NOT a LATIN-1 character ... but a Unicode character that looks like the corresponding LATIN-1 character when displayed.

2 - See Why is the Java main method static? for explanation of why the method is required to be static.

3 - String must correspond to java.lang.String and not a custom class named String hiding it.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。