GVKun编程网logo

为什么将null转换为Object?(将null转化为0)

4

在这里,我们将给大家分享关于为什么将null转换为Object?的知识,让您更了解将null转化为0的本质,同时也会涉及到如何更有效地Angular2WebpacklazyloadTypeError:

在这里,我们将给大家分享关于为什么将null转换为Object?的知识,让您更了解将null转化为0的本质,同时也会涉及到如何更有效地Angular2 Webpack lazyload TypeError:无法将undefined或null转换为object、c# – “null如果object为null,则为null;如果object不为null,则为object.member”、Calendar.before(Object when),为什么是Object?、delphi – 如何将TObject转换为TObjectList?的内容。

本文目录一览:

为什么将null转换为Object?(将null转化为0)

为什么将null转换为Object?(将null转化为0)

我发现一些代码我工作的地方的点null被强制转换Object,因为它是传递给方法。

为什么要这样做?

我知道这个问题涉及重载的方法,并使用类型转换来确定要调用的方法的版本。

但是,如果不执行强制类型转换,Object如果使用空参数调用该方法,那么是否会重选带有其他类型的参数的重载方法呢?那么演员阵容还能完成什么呢?

答案1

小编典典

如果 执行转换,则将选择 最具体的 版本。

null可以是type String或type 的空引用Object。因此,如果这两个方法可用,则将String调用该方法。

如果你有方法ObjectInteger
String随后调用与null(无投)将给出一个编译错误,因为IntegerString都是有效的, 同样特定
(即没有一个其他的一个特例)。在这种情况下,您将 必须 强制转换null以指定要调用的方法。

Angular2 Webpack lazyload TypeError:无法将undefined或null转换为object

Angular2 Webpack lazyload TypeError:无法将undefined或null转换为object

我遇到角度路由器加载器的问题,
我想懒惰加载一个模块,我遵循我看到的每个指南,
并且我不断收到此错误:TypeError:无法将undefined或null转换为object:

The error

这是我的webpack.config.common.js

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: {
        'app': './app/main.ts'
    },resolve: {
        extensions: ['.js','.ts']
    },module: {
        loaders: [
            {
                test: /\.ts$/,loaders: [
                    'awesome-typescript-loader','angular2-template-loader','angular-router-loader'
                ]
            },{
                test: /\.html$/,loader: 'html-loader'
            },{
                test: /\.css$/,loader: 'raw-loader'
            }
        ]
    },plugins: [
        new HtmlWebpackPlugin({
            template: 'index.html'
        }),new webpack.ContextReplacementPlugin(
            // The (\\|\/) piece accounts for path separators in *nix and windows
            /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,'./app' // location of the src
        )
    ]
};

这是app.routing.ts

import { ModuleWithProviders } from '@angular/core';
import { Routes,RouterModule } from '@angular/router';

const appRoutes: Routes = [
    {
        path: '',redirectTo: '/dashboard',pathMatch: 'full'
    },{
        path: 'dashboard',loadChildren: './dashboard/dashboard.module#DashboardModule'
    }
];

export const appRoutingProviders: any[] = [

];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

这是仪表板/仪表板.模块:

import { NgModule }       from '@angular/core';
import { CommonModule }   from '@angular/common';
import { FormsModule }    from '@angular/forms';

import { DashboardComponent }    from './dashboard.component';

import { dashboardRouting } from './dashboard.routing';

@NgModule({
    imports: [
        CommonModule,FormsModule,dashboardRouting
    ],declarations: [
        DashboardComponent
    ],providers: [
    ]
})
export class DashboardModule {}

这是我的依赖项:

"devDependencies": {
    "@types/core-js": "^0.9.35","@types/hammerjs": "^2.0.34","@types/jasmine": "^2.5.38","@types/node": "^6.0.53","angular-router-loader": "^0.5.0","angular2-template-loader": "^0.6.0","awesome-typescript-loader": "^3.0.0-beta.18","del-cli": "^0.2.1","html-loader": "^0.4.4","html-webpack-plugin": "^2.26.0","lite-server": "^2.2.2","raw-loader": "^0.5.1","webpack": "^2.2.0-rc.4","webpack-dev-server": "^2.2.0-rc.0","webpack-merge": "^2.4.0"
  }

我做错了什么?

解决方法

尝试导入

import { RouterModule } from '@angular/router';
import { <children routes of dashboard> } from '<path of dashboard child routes>';

在您的仪表板/ dashboard.module中,然后将其放在DashboardModule的NgModule导入区域中

@NgModule({
    imports: [
        CommonModule,imports: [
        RouterModule.forChild(<children routes of dashboard>)
    ]
})
export class DashboardModule {}

请替换<>部分有必要的细节.

c# – “null如果object为null,则为null;如果object不为null,则为object.member”

c# – “null如果object为null,则为null;如果object不为null,则为object.member”

参见英文答案 > Deep null checking,is there a better way?16个
> How to check for nulls in a deep lambda expression? 10个
我正在尝试编写一个通用扩展方法,让我这样做:
this.startDate = startDateXAttribute.NullOrPropertyOf<DateTime>(() =>
{
    return DateTime.Parse(startDateXAttribute.Value);
});

如果NullOrPropertyOf()在null对象上使用(例如,如果startDateXAttribute为null),则返回null,如果它不为null,则返回Func的结果.

这种扩展方法会是什么样的?

解决方法

没有简短的形式;实现一个是一个相当频繁请求的功能.语法可能类似于:
x = foo.?bar.?baz;

也就是说,如果foo或foo.bar为null,则x为null,如果它们都不为null,则为foo.bar.baz的结果.

我们认为它适用于C#4,但它没有使它在优先级列表的顶部附近.对于该语言的假设未来版本,我们会记住这一点.

更新:C#6将具有此功能.有关设计注意事项的讨论,请参见http://roslyn.codeplex.com/discussions/540883.

Calendar.before(Object when),为什么是Object?

Calendar.before(Object when),为什么是Object?

来自的javadoc Calendar.before(Object when)

返回此Calendar是否代表指定Object所表示的时间之前的时间。此方法等效于:

compareTo(when) < 0

当且仅当when是Calendar实例。否则,该方法返回false。

如果有人传递的不是Calendar实例的东西返回false,为什么它接受一个Object?为什么不只接受Calendar实例呢?这使我在相当长一段时间内一直在监视功能的不正确结果。

答案1

小编典典

我认为没有特别的原因。java.util.Calendar不幸的是,我们不得不解决一些设计问题。

delphi – 如何将TObject转换为TObjectList?

delphi – 如何将TObject转换为TObjectList?

我有一个过程需要将一个TObject数组插入到列表中.该列表可以是任何支持的类型,例如TObjectList,TObjectList< T>,TROArray等

该过程如下所示:

type
  TObjectArray = Array of TObject;

...

procedure TMyClass.DoAssignObjectList(const ObjectArray: TObjectArray;
  const DstList: TObject);
var
  i: Integer;
begin
  if DstList is TObjectList then
  begin
    for i := 0 to pred(TObjectList(DstList).Count) do
      TObjectList(DstList).Add(ObjectArray[i]);
  end else
  if DstList is TObjectList<T> then // ObvIoUsly this doesn't work
  begin
    for i := 0 to pred(TObjectList<T>(DstList).Count) do
      TObjectList<T>(DstList).Add(ObjectArray[i]);
  end
  else
  begin
    raise Exception.CreateFmt(StrNodoAssignorMObject,[DstList.ClassName]);
  end;
end;

如何检查对象是否是TObjectList< T>然后添加一个数组的元素?

解决方法

您必须使用一点RTTI来获取有关泛型类型的更多信息.

下面的代码使用Spring4D,它有一些方法:

uses 
  ...
  Spring.Reflection;

procedure DoAssignObjectList(const ObjectArray: TObjectArray;
  const DstList: TObject);

  function IsGenericTObjectList(const obj: TObject): Boolean;
  var
    t: TRttiType;
  begin
    t := TType.GetType(obj.ClassInfo);
    Result := t.IsGenericType and (t.GetGenericTypeDeFinition = 'TObjectList<>');
  end;

begin
  ...
  if IsGenericTObjectList(DstList) then
  begin
    for i := 0 to pred(TObjectList<TObject>(DstList).Count) do
      TObjectList<TObject>(DstList).Add(ObjectArray[i]);
  ...
end;

除此之外,您还可以获取有关列表的通用参数类型的信息,以检查您放入其中的对象是否符合要求(仅适用于通用类型的课程):

function GetGenericTObjectListParameter(const obj: TObject): TClass;
var
  t: TRttiType;
begin
  t := TType.GetType(obj.ClassInfo);
  Result := t.GetGenericArguments[0].AsInstance.Metaclasstype;
end;

今天关于为什么将null转换为Object?将null转化为0的介绍到此结束,谢谢您的阅读,有关Angular2 Webpack lazyload TypeError:无法将undefined或null转换为object、c# – “null如果object为null,则为null;如果object不为null,则为object.member”、Calendar.before(Object when),为什么是Object?、delphi – 如何将TObject转换为TObjectList?等更多相关知识的信息可以在本站进行查询。

本文标签: