使用java中的方法避免!= null(Avoiding != null using a method in java)
在我的下面的例子中,我想避免每次我想使用getView都写getView!= null。 为了保持清洁,我创建了一个方法hasView()来检查我。 但是我仍然收到警告。 有没有办法解决?
import android.support.annotation.Nullable; public void showView(){ if(hasView()){ getView().show(); // Shows warning Method invocation 'showLoading' may produce 'java.lang.NullPointerException' } } boolean hasView(){ return getView() != null; } @Nullable private View getView(){ return view; }
我正在使用Android Studio / IntelliJ。 我知道我可以使用@SuppressWarnings 我已经看到这个问题,但这使得代码更加丑陋。
In my example below I want to avoid writing getView != null every time I want to use getView. To keep it cleaner I create a method hasView() which does the check for me. However I still get a warning. Is there any way around this?
import android.support.annotation.Nullable; public void showView(){ if(hasView()){ getView().show(); // Shows warning Method invocation 'showLoading' may produce 'java.lang.NullPointerException' } } boolean hasView(){ return getView() != null; } @Nullable private View getView(){ return view; }
I am using Android Studio/IntelliJ. I know that I can use @SuppressWarnings I have seen this question but this makes the code uglier.
原文:https://stackoverflow.com/questions/40567153
最满意答案
详细解释请参阅此文章。
核心代码示例
public static DataTable ToDataTable<T>(this IEnumerable<T> collection) { DataTable dt = new DataTable(); Type t = typeof(T); PropertyInfo[] pia = t.GetProperties(); //Create the columns in the DataTable foreach (PropertyInfo pi in pia) { if ((pi.PropertyType.IsGenericType) ) { Type typeOfColumn = pi.PropertyType.GetGenericArguments()[0]; dt.Columns.Add(pi.Name, typeOfColumn); } else dt.Columns.Add(pi.Name, pi.PropertyType); } //Populate the table foreach (T item in collection) { DataRow dr = dt.NewRow(); dr.BeginEdit(); foreach (PropertyInfo pi in pia) { dr[pi.Name] = pi.GetValue(item, null); } dr.EndEdit(); dt.Rows.Add(dr); } return dt; } }
你为什么认为数据转换会减慢你的应用程序? 你的个人资料呢?
See this artice for detailed explanation.
Core code sample
public static DataTable ToDataTable<T>(this IEnumerable<T> collection) { DataTable dt = new DataTable(); Type t = typeof(T); PropertyInfo[] pia = t.GetProperties(); //Create the columns in the DataTable foreach (PropertyInfo pi in pia) { if ((pi.PropertyType.IsGenericType) ) { Type typeOfColumn = pi.PropertyType.GetGenericArguments()[0]; dt.Columns.Add(pi.Name, typeOfColumn); } else dt.Columns.Add(pi.Name, pi.PropertyType); } //Populate the table foreach (T item in collection) { DataRow dr = dt.NewRow(); dr.BeginEdit(); foreach (PropertyInfo pi in pia) { dr[pi.Name] = pi.GetValue(item, null); } dr.EndEdit(); dt.Rows.Add(dr); } return dt; } }
Why do you think that data converting slows your app? Did your profile it?
相关问答
更多-
该实施没有任何问题。 你可以给yield关键字一个镜头,看看你喜欢它: private IEnumerable
ConvertToTankReadings(DataTable dataTable) { foreach (DataRow row in dataTable.Rows) { yield return new TankReading { ... -
看看这个: 将List / IEnumerable转换为DataTable / DataView 在我的代码中,我将其改为扩展方法: public static DataTable ToDataTable
(this List items) { var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFla ... -
详细解释请参阅此文章。 核心代码示例 public static DataTable ToDataTable
(this IEnumerable collection) { DataTable dt = new DataTable(); Type t = typeof(T); PropertyInfo[] pia = t.GetProperties(); //Create the columns in the DataTab ... -
这些是基本概念: .Net框架附带了几个LINQ提供程序 LINQ to Objects。 System.Linq的。 它是LINQ核心,旨在与内存中的集合一起使用 LINQ to XML。 System.Xml.Linq使用XML文档 LINQ to DataSet。 使用DataTable和DataSet对象 LINQ to SQL。 System.Data.Linq它用于处理SQL Server数据库 LINQ to Entities。 (实体框架)用于处理多个数据库。 它提供了更丰富的API 我认为 ...
-
而不是: row[pi.Name] = t.GetType().InvokeMember(pi.Name, BindingFlags.GetProperty, null, t, null); 使用: row[pi.Name] = pi.GetValue(t, null); Instead of doing: row[pi.Name] = t.GetType().InvokeMember(pi.Name, BindingFlags.GetProperty, null, t, null); use: ro ...
-
将IEnumerable
映射到一个类(Map IEnumerable [2022-03-21]to a class) 正常方法 SomeClass cls = new SomeClass(); Type type = cls.GetType(); foreach (SomeOtherClass somecls in IEnumerableClass) type.GetProperty(somecls.KeyPropName).SetValue(cls, somecls.ValuePropName, null); 如果IEnumerable类是Key Value对集合,则为OR扩展方法 public static ... -
实际上它根本不是指同一个类。 实际上,第二个EmployeeData不引用任何类。 这是通用名称。 您的ODataProxy定义了一个方法: public virtual IEnumerable
GetList (string query); ODataProxyStub使用以下ODataProxyStub实现: public override IEnumerable GetList (string query) 这不是 EmployeeD ... -
你使用linq sql? 在这种情况下,这种方法非常可爱: 使用SQL批量复制与LINQ-to-SQL datacontext partial class MyDataContext { partial void OnCreated() { CommandTimeout = 5 * 60; } public void BulkInsertAll
(IEnumerable entities) { entities = enti ... -
如果您可以编写一个可以使用单个DataRow构造具有“MyProperty”的对象实例的函数,那么您可以这样做: datatable.AsEnumerable().Select(MyConversionFunction); I ended up using Linq to convert the datatable into an array of anonymous objects. Dim objs = From row In dt Select New With { .Id = row.Fi ...
-
问题不在于将List
转换为IEnumerable 。 因为List 实现了IEnumerable 。 您的问题是通用参数不同。 您正在尝试将List 转换为IEnumerable 。 哪里: T1是QlasrService.EntityFramework.tblSoftwareImageTestPlan T2是QlasrService.Model.SchemaModels.LAP.SoftwareImageTestPlan 最简单的解决方案是绘图(手动或自动 )。 自动映 ...